如何让PHP在Bootstrap网格中的5个项目之后插入一个新的div

时间:2013-11-15 21:33:42

标签: php twitter-bootstrap

好的,所以我正在工作的地方,我必须在页面上显示25个项目,但是当我这样做时,它打破了Bootstrap网格系统。如何在编写5个项目后创建一个新的div。

主要PHP代码:

if(isset($_GET['span'])){
        echo "3";
        $grp = $_GET['span'];
        if($grp==1){
            $getGames = "SELECT * FROM tbl_games WHERE tbl_games.games_id < 6";
        }else if ($grp==2){
            $getGames = "SELECT * FROM tbl_games WHERE tbl_games.games_id > 5 AND tbl_games.games_id < 11";
        }else if ($grp==3){
            $getGames = "SELECT * FROM tbl_games WHERE tbl_games.games_id > 10 AND tbl_games.games_id < 16";
        }
        else if ($grp==4){
            $getGames = "SELECT * FROM tbl_games WHERE tbl_games.games_id > 15 AND tbl_games.games_id < 21";
        }else if ($grp==5){
            $getGames = "SELECT * FROM tbl_games WHERE tbl_games.games_id > 20 AND tbl_games.games_id < 26";
        }

     }else if(empty($_GET['userSearch'])){
        echo "4";
        $getGames = "SELECT * FROM tbl_games WHERE games_id <6";//Will Display 5 items
        //$getGames = "SELECT * FROM tbl_games";//Will Display all items
    }

    $getQuery = mysql_query($getGames);
    //Step 2
    $numRows = mysql_num_rows($getQuery);

正文代码中的代码:

<?php
                    if($numRows==0){
                        echo "No go. ".$srch." was not found";
                    }else{
                        while($result =  mysql_fetch_array($getQuery)) {
                            echo "<div class=\"span2\">";
                            echo "<img src=\"images/".$result["games_img"]."\" >";
                            echo "<h3>".$result["games_name"]."</h3>";
                            echo "<p>".$result["games_price"]."</p>";
                            echo "<p>".$result["games_avail"]."</p>";
                            echo "<div>";
                            echo "<a href=\"gamedetails.php?id=".$result["games_id"]."\">
        More Details...</a><br><br>";
                            echo "</div>";
                            echo "</div>";
                        } 
                    }
                ?>

由于

2 个答案:

答案 0 :(得分:3)

在while循环之前添加一个计数器,并使用模数函数检查你是否处于第5循环。

$counter = 0;
while (){
    $counter++;

    // do normal loop stuff here

    if($counter%5 == 0) {
        // add your closing and opening div here

        if($counter == 25) {
            // you might not want to open a new div on the 25th loop
        }

    }

}

答案 1 :(得分:2)

模数运算符非常适合这种情况。做类似的事情:

echo '<div>';
for ($i = 0; $i < 25; $i++) {
    if ($i % 5 === 0 && $i !== 0) {
        echo '</div><div>';
    }

    // Do output here
}
echo '</div>';

这样做每5次打印一次新的div。在您的情况下,跟踪您已完成的记录数,并将其用作比较。