# Need Help Outputting Results Evenly Over Three Columns

Hello,
I am trying to output the results evenly over three columns. I wrote the code below, but it’s not perfect. Look at “Family Wealth and Succession Planning” on this site. http://fw.herrmanneasyedit.com/services/practice-areas You will see when there are four records the third column is empty. Does anyone know of a better solution to this problem? Thanks.

``````
if(\$subpractices){
\$j=0;
\$counter = 1;
\$total_sub = count(\$subpractices);
\$third_of_total = intval(ceil((\$total_sub / 3)));
\$content .= "<ul class=\\"floatlist-3col\\">\
";
while(\$j < \$total_sub){
\$content .= "<li><a href=\\"".\$subpractices[\$j]['practice_id']."-".\$subpractices[\$j]['practice_url_name']."\\" title=\\"".\$subpractices[\$j]['practice_name']."\\">".\$subpractices[\$j]['practice_name']."</a></li>\
";

if(\$counter == \$third_of_total){
\$content .= "</ul>\
";
\$content .= "<ul class=\\"floatlist-3col\\">\
";
\$counter = 0;
}

\$j++;
\$counter++;
}
\$content .= "</ul>\
";
}

``````

Hrm.

The math is probably easier to do if you recalculate progressively.

For instance, with 4, you’d want 2 | 1 | 1.

With 5, You’d want 2 | 2 | 1.

So the ideal way would calculate the 1/3 rounded, use

``````
if(\$subpractices){
\$j=0;
\$total_sub = count(\$subpractices);

while (\$total_sub)
{
\$third = ceil(\$total_sub / 3);
\$total_sub -= \$third;

\$content .= "<ul class=\\"floatlist-3col\\">\
";
for (\$i=\$j; \$j < \$j+third; \$i++)
{
\$content .= "<li><a href=\\"".\$subpractices[\$j]['practice_id']."-".\$subpractices[\$j]['practice_url_name']."\\" title=\\"".\$subpractices[\$j]['practice_name']."\\">".\$subpractices[\$j]['practice_name']."</a></li>\
";
\$j++;
}
\$content .= "</ul>\
";
}
}

``````

This should head you in the right direction hopefully. The code is not tested, so don’t take it literally. The key part is the first 2 lines after while()

On second thought, it should be

\$j=0; \$col=3;

Then

\$third = ceil(\$total_sub / \$col–);

That should get you the right split.

Wonshikee,
Thanks for your help. Are you saying the code should look like the following? When I execute it I get an offset error.

``````
if(\$subpractices){
\$j=0; \$col=3;
\$total_sub = count(\$subpractices);

while (\$total_sub){
\$third = ceil(\$total_sub / \$col--);
\$total_sub -= \$third;

\$content .= "<ul class=\\"floatlist-3col\\">\
";
for (\$i=\$j; \$j < \$j+\$third; \$i++){
\$content .= "<li><a href=\\"".\$subpractices[\$j]['practice_id']."-".\$subpractices[\$j]['practice_url_name']."\\" title=\\"".\$subpractices[\$j]['practice_name']."\\">".\$subpractices[\$j]['practice_name']."</a></li>\
";
\$j++;
}
\$content .= "</ul>\
";
}
}

``````

What is the exact error?