2

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!

1
  • 2
    this has nothing to do with php, and everything to do with css Commented Jul 8, 2013 at 20:24

3 Answers 3

13

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>";
Sign up to request clarification or add additional context in comments.

Comments

3

Here is a simpler way:

<?php $items_per_col = ceil(count( $posts ) / 3); // 3 is columns count ?>

<div class="row">
   <div class="col-sm-4"> <!-- col-sm-4 for 3 columns -->
      <?php foreach ( $posts as $i => $post ): ?>

         <?php if($i % $items_per_col === 0 && $i !== 0): ?>
            </div><div class="col-sm-4"> <!-- col-sm-4 for 3 columns -->
         <?php endif; ?>

         <?php echo $post; ?>

      <?php endforeach; ?>
   </div>
</div>

Comments

1

Here is how I make whatever amount of columns you need.

<? $columns = array(); //prepare array for our columns
$i = 0; //prepare counter
foreach ($posts as $value) {

    // reset the counter when we reach the third column (column 0, column 1, column 3 -> reset -> column 0)
    //change the $i == 3 to $i == 4 if you need 4 columns of whatever amount of columns you need
    if($i == 3){ $i = 0; }

    //this is just for php warning "array_push() expects parameter 1 to be array, null given"
    //if the sub array doesn't exist, make one
    if(!isset($columns[$i])){
        $columns[$i] = array();
    }

    //add the chunk to the column array
    array_push($columns[$i], $value);
    $i++;

} ?>

<? foreach ($columns as $key => $column) { ?>

    <div class="triple-column column<? echo $key //columns are numbered, it's usefull for CSS ?>">
        <? foreach ($column as $post) {

        // Do something with the $post here
        // $post is the single element from the array we had at the beginning

        } ?>
    </div>
<? } ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.