Page 1 of 1

Nesting an If Statement Within a While Loop

Posted: Sat Jun 28, 2008 6:05 pm
by dgny06
Thanks in advance for any help.....

I am doing a real estate site and on my "ShowListings.php" page I want to include a line of text if my "OpenHouse" variable value is true for a given record. The problem is that with my code below the conditional text does not loop with each record if the value is true for more than one, but instead it just repeats the given amount of times before my loop starts (at the top of the page). Can somebody help me set up the loop correctly (lines 25-28 below show contain the conditional text)?

Code: Select all

<table align="center">
 
<?PHP
// This query returns partial information for listings that are both not voided and not sold 
 
$ListingsQuery = "select l.ListingID as ListID, l.Image, l.tbType_TypeID, t.TypeName, b.Borough, l.City, l.Price, l.Rooms, l.OpenHouse
from tbListing l
join tbBorough b on b.BoroughID = l.tbBorough_BoroughID
join tbType t on t.TypeID = l.tbType_TypeID
where l.Void = '0'
and l.Sold = '0'";
$FindListings = mysql_query($ListingsQuery);
 
while ($ReturnedListings = mysql_fetch_assoc($FindListings))
{
$Image = $ReturnedListings['Image'];
$ListingID = $ReturnedListings['ListID'];
$TypeName = $ReturnedListings['TypeName'];
$Borough = $ReturnedListings['Borough'];
$City = $ReturnedListings['City'];
$Price = $ReturnedListings['Price'];
$Rooms = $ReturnedListings['Rooms'];
$OpenHouse = $ReturnedListings['OpenHouse'];
 
if ($OpenHouse == "1")
{
echo "<p align=\"center\">Open House Scheduled</p>";
}
 
echo "<tr><td colspan=\"2\">" . "<a href=\"ListingInfo.php?ListingID=$ListingID\"><img src=\"$Image\" border=\"0\"></a>" . "</td></tr>";
echo "<tr><td>ListingID:</td><td><a href=\"ListingInfo.php?ListingID=$ListingID\">$ListingID</a></td></tr>";
echo "<tr><td>Type:</td><td>$TypeName</td></tr>";
echo "<tr><td>Borough:</td><td>$Borough</td></tr>";
echo "<tr><td>City:</td><td>$City</td></tr>";
echo "<tr><td>Rooms:</td><td>$Rooms</td></tr>";
echo "<tr><td>Price:</td><td>" . "$" . number_format($Price, 2) . "</td></tr>";
echo "<tr><td height=\"25px\">&nbsp;</td></tr>";
}
 
?>
 
</table>

Re: Nesting an If Statement Within a While Loop

Posted: Sat Jun 28, 2008 6:50 pm
by califdon
The issue is with your HTML. After you start a <table> and before the </table> tag, everything must be enclosed in <tr> and <td> tags. If it is not, it will not be displayed as part of the table, but displayed above it, as you have observed.

So, you have to decide just where you want it to appear; at the beginning of the entry? at the end of the entry? Wherever you want it to be, that's where you put your if statement, but not as a <p> tag, as another <tr><td> ... </td></tr> tag.

Re: Nesting an If Statement Within a While Loop

Posted: Sat Jun 28, 2008 7:53 pm
by dgny06
I should have realized that......thanks!!!