Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert <br/> after each 5 results?

Tags:

php

mysql

This is my code:

$query = mysql_query("SELECT * FROM books ORDER BY id") or die(mysql_error());  
while($row = mysql_fetch_assoc($query)) {
echo $row["bookname"]." - ";
}

How to make only 5 books displayed in each line, by inserting a
at the start if the row is 5 or 10 or 15 etc...

Thanks

like image 240
CodeOverload Avatar asked Dec 23 '22 03:12

CodeOverload


1 Answers

You could keep count of the times you've looped (increment a variable each time).

Compare the value modulus 5. If the result is 0 output the

$rowCounter = 1;

while($row = mysql_fetch_assoc($query)) {
    echo $row["bookname"]." - ";

    if( $rowCounter % 5 == 0 ) {
        echo "<hr />";
    }

    $rowCounter++;
}
like image 119
Michael Shimmins Avatar answered Jan 11 '23 03:01

Michael Shimmins