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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.