4

I have a foreach loop and my code looks this is:

foreach($items as $item){
    echo $item['title'];
}

For example I have 15 items in my loop. My loop will output something like this:

item 1
item 2
item 3
item 4
item 5
item 6
item 7
item 8
item 9
item 10
item 11
item 12
item 13
item 14 
item 15

How can I show my results in 3 columns like the example below in a div or table?

    item 1       item 6      item 11
    item 2       item 7      item 12
    item 3       item 8      item 13
    item 4       item 9      item 14
    item 5       item 10     item 15
1
  • i don't know how can do it Commented Sep 27, 2014 at 16:22

4 Answers 4

5

You can use array_chunk to split into the number of groups you desire then use twitter bootstrap to place your items into a div like

<div class="row">
$all_items = array_chunk($my_array,3);

foreach($all_items as $div_item){
foreach ($div_item as $col_md_4_item){
echo '<div class="col-md-4">';
echo $col_md_4_item;
echo '</div>';
}
}
</div>
Sign up to request clarification or add additional context in comments.

Comments

5

if you want the results in a table:

$i = 0;

echo '    <table>
            <tr>';
foreach($items as $item){
    $i++;
    echo '<td>'.$item['title'].'</td>';

    if($i == 3) { // three items in a row. Edit this to get more or less items on a row
        echo '</tr><tr>';
        $i = 0;
    }
}
echo '    </tr>
        </table>';

This code should atleast give you a place to start.

4 Comments

@user3246727 Yep I just noticed it. And the <tr></tr> which should have been </tr><tr> which I also fixed
@user3246727 How can this answer your question? This does not generate the same output as in your example. The output will be item 1 item 2 item 3 in one line.
@wumm yes but after the third item it will go to another which is what the OP wants
@SuperDJ Look at the question closely; do you see it says item 1 item 6 item 11? (However I did a minor edit to be able to remove the down vote)
3

Try this if your PHP version 5.5+

$title_array = array_column($items, 'title');
$chunkArr = array_chunk($title_array, count($title_array) / 3);
$count = count(current($chunkArr));
$table = '<table border="1">';
for($i = 0; $i < $count; $i++){
    $table .= '<tr>';
    foreach($chunkArr as $val){
        $table .= '<td>'.$val[$i].'</td>';
    }
    $table .= '</tr>';
}
$table .= '</table>';
echo $table;

Comments

3
<style type="text/css">
.item {
    float:left
}
</style>
<?php
$data = array('item 1','item 2','item 3','item 4','item 5','item 6','item 7','item 8','item 9','item 10','item 11','item 12','item 13','item 14 ','item 15',);
$num_item = 5; //we set number of item in each col
$current_col = 0;
$v = '';
foreach ($data as $item) {

    if ($current_col == 0) {
        $v .= '<div class="item">
        <ul>';   
    }
    //$image = preg_replace('/images/','_thumbs/Images',$p->image);
    $v .= ' <li>'.$item.'</li>';
    if ($current_col == $num_item - 1) { // Close the row if $current_col equals to 2 in the example  ($num_cols -1)
        $current_col = 0;
        $v .= '</ul></div>';    
    } else {
        $current_col++;            
    }                
}
$v .= '</div>';
echo $v;
?>

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.