Destramic Posted May 21, 2010 Share Posted May 21, 2010 hello...i have a while loop which works fine but i want to count and every 5th loop have a break (<br />)....im just wondering on the best way of doing is...i have on method...but i'm thinking there may be a better way of doing it than the way i am? <?php $i = 0; while ($games_row = mysql_fetch_array($games_result, MYSQL_BOTH)) { if ($i =5 ) { echo "<br />"; $i = 0; } printf ("<option value=\"\">%s</option>\n", $game_name); $i++; } ?> thanks Quote Link to comment https://forums.phpfreaks.com/topic/202510-while-loop-breaks/ Share on other sites More sharing options...
kenrbnsn Posted May 21, 2010 Share Posted May 21, 2010 That loop will not work if it's coded like you've written it. This <?php if ($i =5 ) ?> is not correct and should be <?php if ($i == 5) ?> The way most people write this is to use the Modulus operator, %: <?php $i = 1; while ($games_row = mysql_fetch_array($games_result, MYSQL_BOTH)) { if ($i % 5 == 0) { echo "<br />"; } echo "<option value=''>$game_name</option>\n"; $i++; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/202510-while-loop-breaks/#findComment-1061635 Share on other sites More sharing options...
Destramic Posted May 21, 2010 Author Share Posted May 21, 2010 thats great thanks ken Quote Link to comment https://forums.phpfreaks.com/topic/202510-while-loop-breaks/#findComment-1061678 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.