if...else within while statement...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
orbdrums
Forum Commoner
Posts: 82
Joined: Wed Sep 14, 2011 11:42 pm

if...else within while statement...

Post by orbdrums »

I can't get an if...else construct to execute within a while statement. Here is the code with the suspect code commented out. The code works as is but if I try uncommenting the if...else php returns a blank page. Thanks in advance for your assistance.

$r_n = 1;
while ($row = mysql_fetch_assoc($result))
{
// if ($r_n <= 9)
// {
echo "<br />",'Record:',$r_n,"&nbsp;&nbsp;&nbsp;",'Name: ',$row['First name'];
echo ' ';
echo $row['Last name'];
echo ' ';
echo 'Phone: ',$row['Phone'];
echo ' ';
echo 'Email: ',$row['Email'];
echo ' ';
$r_n++;
// else
// echo "<br />",'Record:',$r_n,"&nbsp;&nbsp;",'Name: ',$row['First name'];
// echo ' ';
// echo $row['Last name'];
// echo ' ';
// echo 'Phone: ',$row['Phone'];
// echo ' ';
// echo 'Email: ',$row['Email'];
// echo ' ';
// $r_n++;
// }
}
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: if...else within while statement...

Post by Eric! »

Your curly brackets are missing. It should look like this

Code: Select all

$r_n = 1;
while ($row = mysql_fetch_assoc($result)) {
    if ($r_n <= 9) {
        echo " ", 'Record:', $r_n, "&nbsp;&nbsp;&nbsp;", 'Name: ', $row['First name'];
        echo ' ';
        echo $row['Last name'];
        echo ' ';
        echo 'Phone: ', $row['Phone'];
        echo ' ';
        echo 'Email: ', $row['Email'];
        echo ' ';
        $r_n++;
    } else {
        echo " ", 'Record:', $r_n, "&nbsp;&nbsp;", 'Name: ', $row['First name'];
        echo ' ';
        echo $row['Last name'];
        echo ' ';
        echo 'Phone: ', $row['Phone'];
        echo ' ';
        echo 'Email: ', $row['Email'];
        echo ' ';
        $r_n++;
    }
}
orbdrums
Forum Commoner
Posts: 82
Joined: Wed Sep 14, 2011 11:42 pm

Re: if...else within while statement...

Post by orbdrums »

You are the man! Thanks so much for the help. It was driving me crazy. I'm brand new to php so I make stupid mistakes just like that one.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: if...else within while statement...

Post by Eric! »

no problem! If you forget the curly brackets only the next line is part of the conditional, everything else will get executed outside of your conditional statement. Also don't forget to use [ syntax=php ]PASTE CODE HERE[ /syntax ] when posting
Post Reply