Jump to content

while() loop breaks


Destramic

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/202510-while-loop-breaks/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/202510-while-loop-breaks/#findComment-1061635
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.