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," ",'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," ",'Name: ',$row['First name'];
// echo ' ';
// echo $row['Last name'];
// echo ' ';
// echo 'Phone: ',$row['Phone'];
// echo ' ';
// echo 'Email: ',$row['Email'];
// echo ' ';
// $r_n++;
// }
}
if...else within while statement...
Moderator: General Moderators
Re: if...else within while statement...
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, " ", '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, " ", '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...
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...
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