Jump to content
Larry Ullman's Book Forums

Selected Suburb Not Displaying Correctly


Recommended Posts

When I run the following code it runs through the entire list of suburbs and only displays the last one rather than the selceted suburb.

 

What am I doing wrong?

 

 

<?php
 $query="select * from tblStaff where StaffID=$staffID";
$result=@mysqli_query($dbc,$query);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);

$selected_suburb=$row['Suburb'];

echo"<label>Suburb</label><br />";
echo"<select name=\"Suburb\">";
$query="select suburb_id, suburb, state, postcode
from tblPostcodes
order by suburb,state, postcode asc;";
$result=@mysqli_query($dbc,$query);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
 	 $selected="";
 	 foreach($row as $suburb_id => $suburb){
 	 if($row['suburb_id']==$selected_suburb){
 	 $selected="selected";
 	 }
 echo"<option value={$row['suburb_id']}
 selected=$selected>
 {$row['suburb']} {$row['state']} {$row['postcode']}</option>";
 	 }
}
echo"</select>";
?>

Link to comment
Share on other sites

Probably not the issue, but I don't understand the point of the foreach loop in the while loop.

I'd get rid of the foreach loop and see if that solves the problem.

In general, your logic seems a bit off, so I'd tidy up the code first and foremost.

By doing so, you'll hopefully solve the problem on your own.

  • Upvote 1
Link to comment
Share on other sites

OK, I've taken oput the foreach loop so the code now looks like this:

<?php
                        	 echo"<label>Suburb</label><br />";
                           echo"<select name=\"Suburb\">";
                               $query="select suburb_id, suburb, state, postcode
                                       from tblPostcodes
                                       order by suburb,state, postcode asc;";
                               $result=@mysqli_query($dbc,$query);
                               while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
                                   if($row['suburb_id']==$suburb_id){
                                       $selected="selected";                                            
                                   }else{
                                       $selected="";
                                   }
                                   echo"<option value=\"{$row['suburb_id']}\" selected=$selected>
                                                {$row['suburb']}  {$row['state']} {$row['postcode']}</option>";
                               }
                               echo"</select>";
?>

 

But it still goes straight to the last suburb in the list.

 

Any help will be appreciated.

Link to comment
Share on other sites

Yes, I agree with Larry.

Also, while I can't be sure without knowing more about the DB structure you're using, it seems like you should be able to perform some sort of join to make it so you only need one DB query instead of two.

 

If you're willing to tell us a bit more about what you're looking to accomplish, we could maybe help you more.

Link to comment
Share on other sites

The problem turned out to be that the line:

echo"<option value=\"{$row['suburb_id']}\" selected=$selected>{$row['suburb']}  {$row['state']} {$row['postcode']}</option>";

 

Should have read:

echo"<option value=\"{$row['suburb_id']}\" $selected>{$row['suburb']}  {$row['state']} {$row['postcode']}</option>";

i.e. without the "selected="

 

Thanks for your help. All is now good.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...