Jump to content

while loop inside a while loop


alienmojo

Recommended Posts

$con = mysql_connect("localhost","root","******");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }


mysql_select_db("realestate", $con);

$table=$_POST[username];
$result = mysql_query("SELECT * FROM $table");

echo "<table border='5'>
  <tr>
  <th>Picture</th>
  <th>Address</th>
  <th>Price</th>
  <th>Baths</th>
  <th>Bedrooms</th>
  <th>State</th>
  <th>City</th>
  <th>Square Feet</th>
  </tr>";

while($row =  mysql_fetch_array($result))
$ac=$row['fav'];
{
$result1 = mysql_query("SELECT * FROM estate WHERE accountnum=$ac");

while($row =  mysql_fetch_array($result1))
  {
  $accountnum=$row['accountnum'];  

  echo "<tr>";
  echo '<td><a href="descrip.php?accountnum='.$accountnum.'"> <img src="images/'.$row['imagename'].'" width="75" height="75"></a> </td>';
  echo "<td>" . $row['address'] . "</td>";
  echo "<td>" . $row['price'] . "</td>";
  echo "<td>" . $row['baths'] . "</td>";
  echo "<td>" . $row['bedrooms'] . "</td>";
  echo "<td>" . $row['state'] . "</td>";
  echo "<td>" . $row['city'] . "</td>";
  echo "<td>" . $row['squarefeet'] . "</td>";
  echo "</tr>";
  }
}

if(mysql_num_rows($result1) == 0)
ECHO "<h3>YOU HAVE SAVED NO FAVORITES!</h3>";
mysql_close($con);


 

what im doing is writing a php script that displays a members fav houses i did this by when the user finds a house he likes it stores the account number in "there" table this is sopost to run through all of the account number store in the members table and then referance that number to account number in another table and output the matches in a table. it works fine except instead outputing all of the house it only outputs the house entered last. any question just ask

Link to comment
https://forums.phpfreaks.com/topic/37569-while-loop-inside-a-while-loop/
Share on other sites

OMG that was an incredible run on sentence! I've corrected it just so others can understand it:

 

Original Question:

What I'm doing is writing a PHP script that displays a members fav houses. I did this by when the user finds a house he likes, it stores the account number in "their" table. This is supposed to run through all of the account numbers stored in the member's table and then reference that number to an account number in another table. The script then outputs the matches into an HTML table. It works fine, except instead of outputting all of the houses, it only outputs the house entered last. any question just ask

 

First off, it looks like you've got a couple lines flipped ->

 

while($row =  mysql_fetch_array($result))

$ac=$row['fav'];

{

$result1 = mysql_query("SELECT * FROM estate WHERE accountnum=$ac");

 

SHOULD READ:

 

while($row =  mysql_fetch_array($result))

{

$ac=$row['fav'];

$result1 = mysql_query("SELECT * FROM estate WHERE accountnum=$ac");

 

Also, that last block of code looks a bit wacky to me:

 

if(mysql_num_rows($result1) == 0)

ECHO "<h3>YOU HAVE SAVED NO FAVORITES!</h3>";

mysql_close($con);

 

If your messing with multiple lines in a conditional, it's better practice to use curly braces to define your statements.

 

 

Hope that helps

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.