Nesting an If Statement Within a While Loop

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
dgny06
Forum Commoner
Posts: 25
Joined: Thu Jun 14, 2007 11:35 pm

Nesting an If Statement Within a While Loop

Post 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>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Nesting an If Statement Within a While Loop

Post 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.
dgny06
Forum Commoner
Posts: 25
Joined: Thu Jun 14, 2007 11:35 pm

Re: Nesting an If Statement Within a While Loop

Post by dgny06 »

I should have realized that......thanks!!!
Post Reply