I have two table classifieds and state.
classifieds – id, title, state_id
state – id, statename
I am trying to echo out the statename from state. I’ve tried this query but it echos only the last row.
<?php
$query = "SELECT * FROM classifieds ";
$result = mysql_query($query) or die(mysql_query());
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<div>
<span>Title: <?php echo $row['title'];?> </span>
</div>
<div>
<span>Name: <?php echo $row['name'];?> </span>
</div>
$q = mysql_query("SELECT * FROM classifieds AS C
LEFT JOIN state AS S
ON C.state_id = S.id");
$z = array();
while($state=mysql_fetch_array($q))
{
array_push($z,$state["statename"]);
}
?>
<div>
<span>State: <?php foreach($z as $location) { echo $location; } ?></span>
</div>
<?php } // End of the first while loop ?>
Thanks
You need to move your
echocommand inside thewhileloop. Otherwise it will only echo once it has gone through all of the rows.Edit: With your updated code, I can see that your problem is that you first get a list of all the
classifieds, then that same list again with the states joined in. You don’t need to do the first query at all, only the second one.