Jump to content
Larry Ullman's Book Forums

Pull Down Menu Not Showing Select Item From Database


Recommended Posts

Hi there,

 

I have an edit details page on my website (php with a mysql database).

 

It contains text boxes which display the information which the user has submitted at an earlier date. If they edit the information in the textbox and click on the submit button, it will update in the database.

 

These work fine without any problems.

 

I now want to add a pulldown menu which will work in the same way by displaying the information that the user saved on a previous visit (but obviously they can choose other options and click submit to save). My problem is that I am only able to show either the first or the list item on the list when the page opens and not the item that the user has selected on the previous visit.

 

Any help would be appreciated.

echo '<tr><td>Change Timeframe</td><td> <select name="checklist_timeframe_id">';	

//query that will display all the options that the user can select    $cq = "SELECT * FROM checklist_timeframe";
	 $cr=mysqli_query($dbc,$cq) or die(mysqli_error($dbc)." Q=".$cq);
//if the query runs successfully    if ($cr)
 {
  while ($ca = mysqli_fetch_array($cr, MYSQLI_ASSOC))
  {

  echo '<option value="' . $ca['checklist_timeframe_id'];

  if ($info['checklist_timeframe_id'] ==$ca['checklist_timeframe_id'])
  {
  echo '"selected="selected';
  }
  echo '">' . $ca['checklist_timeframe'] . '</option>';
  mysqli_free_result($cr);//free up resources
 }
 }
 else {
  echo '</select>We apologise there seems to be a problem with this data';
 }
 //echo '</select>';

Link to comment
Share on other sites

Try

 

 

while ($ca = mysqli_fetch_array($cr, MYSQLI_ASSOC)) {

         echo '<option value="' . $ca['checklist_timeframe_id'] . '"';

         if ($info['checklist_timeframe_id'] ==$ca['checklist_timeframe_id']) {
          echo ' "selected="selected"';
         }
         echo '>' . $ca['checklist_timeframe'] . '</option>';
         mysqli_free_result($cr);//free up resources
}

  • Upvote 1
Link to comment
Share on other sites

Woops, typo, try:

 

 

while ($ca = mysqli_fetch_array($cr, MYSQLI_ASSOC)) {

echo '<option value="' . $ca['checklist_timeframe_id'] . '"';

if ($info['checklist_timeframe_id'] == $ca['checklist_timeframe_id']) {

echo ' selected="selected"';

}

echo '>' . $ca['checklist_timeframe'] . '</option>';

mysqli_free_result($cr);//free up resources

}

  • Upvote 2
Link to comment
Share on other sites

  • 2 weeks later...

Thanks for your help. It worked but only when I realised I had closed off the main while loop before this section of code so it couldn't read the $info['checklist_timeframe_id'] so I moved the closing curly bracket to the end of the above section of code and it worked (I had it placed before the above section). A silly mistake on my part! :D

 

thanks

Link to comment
Share on other sites

 Share

×
×
  • Create New...