Using PHP loop to add Bootstrap rows and proper column numbers to elements

后端 未结 6 1617
一整个雨季
一整个雨季 2020-12-08 22:06

I\'m trying to create the following front-end using a PHP loop and Twitter Bootstrap\'s 12 column grid system:

The HTML output is:

6条回答
  •  时光取名叫无心
    2020-12-08 22:35

    Everytime I need to to this, I just use array_chunk to build a proper array chunk for my rows and columns.

    For example You have:

    $posts = [['id' => 1], ['id' => 2] ...]

    Instead of looping and calculating whether to add row, make chunks of your posts:

    $posts = [['id' => 1], ['id' => 2] ...]
    
    $postChunks = array_chunk($posts, 4); // 4 is used to have 4 items in a row
    foreach ($postChunks as $posts) {
        
    foreach ($posts as $post) {
    }
    }

提交回复
热议问题