sandari Posted December 24, 2012 Share Posted December 24, 2012 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 More sharing options...
HartleySan Posted December 24, 2012 Share Posted December 24, 2012 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. 1 Link to comment Share on other sites More sharing options...
sandari Posted December 25, 2012 Author Share Posted December 25, 2012 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 More sharing options...
benjamin.morgan Posted December 26, 2012 Share Posted December 26, 2012 $query="select suburb_id, suburb, state, postcode from tblPostcodes order by suburb,state, postcode asc;"; Not sure if it is because of the semicolon right there, but I don't think it is supposed to be there. I didn't use code tags so I could emphasize the semicolon. Link to comment Share on other sites More sharing options...
Larry Posted December 26, 2012 Share Posted December 26, 2012 Have you confirmed the query results? Also, your logic for making it sticky is not optimal and could create bugs as it printed selected= for all inputs. Link to comment Share on other sites More sharing options...
HartleySan Posted December 26, 2012 Share Posted December 26, 2012 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 More sharing options...
sandari Posted December 27, 2012 Author Share Posted December 27, 2012 Thanks for your suggestions. I will look into it but I am starting to think the problem lies further back up in the code. Link to comment Share on other sites More sharing options...
sandari Posted December 27, 2012 Author Share Posted December 27, 2012 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. 1 Link to comment Share on other sites More sharing options...
HartleySan Posted December 27, 2012 Share Posted December 27, 2012 Glad it works. You may want to look further into implementing the suggestions Larry and I made though, as it will optimize the code. Link to comment Share on other sites More sharing options...
Recommended Posts