I'm using the following PHP code to echo the data from an array...
foreach($businessnames as $b)
{
$theurl = get_term_link($b, 'businessnames');
echo "<li style='width: 31%;'><a href=\"" . $theurl . $append . "\">". $b->name ."</a></li>";
}
I was using this to get the data into 3 equal columns but now I need to do 3 equal columns in the order from the array (as opposed to "left to right" like CSS floats cause). I imagine I need to count the total values in the array with PHP and then divide that, but I'm not sure what the code would be. I've been looking around but haven't found something that addresses this in particular.
How could I adapt my code here to put the array's data into 3 equal columns?
Thanks!
You can use array_chunk to split an array into pieces
$cols = array_chunk($businessnames, ceil(count($businessnames)/3));
foreach ($cols as $businessnames){
echo "<ol class='col'>";
foreach($businessnames as $b)
{
$theurl = get_term_link($b, 'businessnames');
echo "<li style='width: 31%;'><a href=\"" . $theurl . $append . "\">". $b->name ."</a></li>";
}
echo "</ol>";
}
Alternatively you can use a pure css solution.
echo "<ol style='column-count:3; -o-column-count:3; -moz-column-count:3; -webkit-column-count:3;'>";
foreach($businessnames as $b)
{
$theurl = get_term_link($b, 'businessnames');
echo "<li style='width: 31%;'><a href=\"" . $theurl . $append . "\">". $b->name ."</a></li>";
}
echo "</ol>";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With