简体   繁体   中英

How to make PHP insert a new div after 5 Items in Bootstrap grid

Okay so I'm working where I have to display 25 items on a page but when I do this it breaks the Bootstrap grid system. How do I have PHP create a new div after 5 items are written.

Main PHP Code:

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);

Code in Body Tag:

<?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>";
                        } 
                    }
                ?>

Thanks

Add a counter just before the while loop and use a modulo function to check wether you are on a 5th loop.

$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
        }

    }

}

The modulus operator is excellent for a situation like this. Do something like:

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

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

What this does is print new divs every 5 times. In your case, keep track of the number of records you've done, and use that as the comparison.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM