Page 1 of 1

if...else within while statement...

Posted: Thu Sep 15, 2011 3:44 pm
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++;
// }
}

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

Posted: Thu Sep 15, 2011 5:15 pm
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++;
    }
}

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

Posted: Thu Sep 15, 2011 9:29 pm
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.

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

Posted: Sun Sep 25, 2011 7:52 am
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