osiris1603 Posted July 16, 2008 Share Posted July 16, 2008 Hi, I have this so that the select drop down menu gives the option/value of the "status" so it is selected in the drop down menu, so if the status was "Processing" then the first option shown in the <select> would be "Processing" instead of the pre-defined first option "Pending". Although, I am not sure why this is not giving the correct output, as it still stays at "Pending"... Does anyone know how I can resolve this? <?php $status = "SELECT status FROM orders WHERE id = $id"; $orderstat = mysql_query ($status); while ($get_info = mysql_fetch_row($orderstat)){ foreach ($get_info as $orderstatus) print "$orderstatus"; } $options = array ("Pending", "Processing", "Completed", "Dispatched", "Not Cleared", "Cancelled"); $status = mysql_query ("SELECT status FROM orders WHERE id = ". $id); $get_info = mysql_fetch_row($orderstat); $orderstatus = $get_info[0]; $form = '<select name="status">'."\n"; foreach ( $options as $option ) { $selected = ( $orderstatus == $option ) ? ' selected="selected"' : ''; $form .= '<option value="'. $option .'"'. $selected .'>'. $option .'</option>'."\n"; } $form .= '</select>'; print $form; ?> Many thanks in advance... Link to comment https://forums.phpfreaks.com/topic/114989-solved-ltselectgt-remember-selected-option/ Share on other sites More sharing options...
Xurion Posted July 16, 2008 Share Posted July 16, 2008 I simply use the following: echo '<select name="myselect"> <option value="myvalue"'; if($orderstatus == $option) echo ' selected="selected"'; echo '>My Option</option> </select>'; Link to comment https://forums.phpfreaks.com/topic/114989-solved-ltselectgt-remember-selected-option/#findComment-591377 Share on other sites More sharing options...
osiris1603 Posted July 16, 2008 Author Share Posted July 16, 2008 I simply use the following: echo '<select name="myselect"> <option value="myvalue"'; if($orderstatus == $option) echo ' selected="selected"'; echo '>My Option</option> </select>'; Thank you for your help, although I changed the value of the get_info <?php $status = "SELECT status FROM orders WHERE id = $id"; $orderstat = mysql_query ($status); while ($get_info = mysql_fetch_row($orderstat)){ foreach ($get_info as $orderstatus) print "$orderstatus"; } $options = array ("Pending", "Processing", "Completed", "Dispatched", "Not Cleared", "Cancelled"); $status = mysql_query ("SELECT status FROM orders WHERE id = ". $id); $get_info = mysql_fetch_row($status); // changed this line $orderstatus = $get_info[0]; $form = '<select name="status">'."\n"; foreach ( $options as $option ) { $selected = ( $orderstatus == $option ) ? ' selected="selected"' : ''; $form .= '<option value="'. $option .'"'. $selected .'>'. $option .'</option>'."\n"; } $form .= '</select>'; print $form; ?> Link to comment https://forums.phpfreaks.com/topic/114989-solved-ltselectgt-remember-selected-option/#findComment-591396 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.