How to insert <br/"> after each 5 results?
5 answers
You can count the number of cycles that you looped (increase the value of the variable each time).
Compare the absolute value of 5. If the result is 0, print <
$rowCounter = 1; while($row = mysql_fetch_assoc($query)) { echo $row["bookname"]." - "; if( $rowCounter % 5 == 0 ) { echo "<hr />"; } $rowCounter++; } +7
A shorter version of Michael's code:
$rowCounter = 0; while ($row = mysql_fetch_assoc($query)) { echo $row["bookname"] . ' - '; if (++$rowCounter % 5 == 0) { echo '<br />'; } } Another alternative:
$rowCounter = 1; // not sure if this should be 1 or 0 // 1 is correct, check comments while ($row = mysql_fetch_assoc($query)) { echo $row["bookname"] . ' - '; if ($rowCounter++ % 5 == 0) { echo '<br />'; } } 0