在while循环中分配DIV

时间:2015-11-29 18:58:58

标签: php sql

大家好,并提前致谢。所以我有一个看起来像这样的while循环:

<?php
    $limit = 4;
    $countSql = "SELECT COUNT(book_id) FROM books";
    $tot_result = mysqli_query($conn, $countSql);
    $row = mysqli_fetch_row($tot_result);
    $total_records = $row[0];
    $total_pages = ceil($total_records / $limit);
    if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };
    $start_from = ($page-1) * $limit;
    $sql = "SELECT * FROM books ORDER BY book_id DESC LIMIT $start_from, $limit";
    $rs_result = mysqli_query($conn, $sql);
    while ($row = mysqli_fetch_assoc($rs_result)) {
        $book_titlel=$row['book_titlel'];
        $book_titlel=sanitize($book_titlel);
        echo "<div class='grid_2 col-md_4'><div class='box_9'><div class='img-wrap'><a href='book/".$book_titlel."/".$row['book_id']."'><img alt='".$row['book_title']."' data-src='images/books/".$row['book_picture'].".jpg' src='images/preloader.gif'></a></div><div class='caption'><div class='rating-bar'><div class='rating' style='width: 50%'></div></div><h3 class='text_5 color_1'><a href='book/".$book_titlel."/".$row['book_id']."'>".$row['book_title']."</a></h3><p class='text_6 color_3'><a href='book/".$book_titlel."/".$row['book_id']."'>".$row['book_author']."</a></p></div></div></div>";
    };
?>

所以我要做的就是每4个结果分配一个DIV;例如: 1到4的结果将被包裹在<div class='row'></div>中,从5到8包含在新的<div class='row'></div>中,从9到12也包含在内,依此类推。有可能吗?

2 个答案:

答案 0 :(得分:0)

在你的循环前面我把你的包装器打开div然后在每个第四个进入循环中它将关闭div并打开一个新的

ViewGroup

答案 1 :(得分:0)

你可以试试这个:

<?php

...

$temp_count = 0;

echo "<div class='row'>";

while($row = mysqli_fetch_assoc($rs_result)){

    if($temp_count  == 4){
        echo "</div><div class='row'>";
        $a = 0;
    }

    echo {YOUR_DATA};
    $temp_count++;


}

echo "</div>";

?>