ChrisDN Posted June 10, 2008 Share Posted June 10, 2008 I've had a search around and I've not been able to find anything specifically relating to this and been unable to piece anything together. I'm wanting to have a dropdown box in my form populated with options within the usual select tag but also have whatever option was saved originally in my MySQL table as the selected option. I have at the moment: table name: combo field name: dropdown This is the only field in the table that I set up for testing this. $query = mysql_query("SELECT dropdown FROM combo") or die(mysql_error()); echo "<form action='action' method='post'>"; echo "<select name='option'>"; while ($nt = mysql_fetch_array($query)) { echo "<option value=''>".$nt['dropdown']."</option>"; } echo "</select><br />"; echo "<input type='submit' value='Submit'>"; echo "</form>"; But this is obviously only placing the one option in the dropdown which is of course, pointless. Could anyone please point me in the right direction? Quote Link to comment https://forums.phpfreaks.com/topic/109576-solved-dropdown-box-populated-mysql-field-selected/ Share on other sites More sharing options...
saint959 Posted June 10, 2008 Share Posted June 10, 2008 Hey, i think this is something that your talking about. From what i understand it is something that i have actually needed to do quite often. <form name="form1"> Select your action: <select name="option_show"> <?php $saved_orig = "whatever option was saved originally in my MySQL table "; $query = mysql_query("SELECT dropdown FROM combo") or die(mysql_error()); while ($nt = mysql_fetch_assoc($query)) { echo "<option value=\"$dropdown\""; if ($dropdown == $saved_orig){ echo "selected";} echo">".$nt['dropdown']."</option>"; } ?> </select> </form> hope it helps Quote Link to comment https://forums.phpfreaks.com/topic/109576-solved-dropdown-box-populated-mysql-field-selected/#findComment-562035 Share on other sites More sharing options...
ChrisDN Posted June 10, 2008 Author Share Posted June 10, 2008 Thanks for the swift reply Saint. After trying to wrap my brain round that, I have assumed that I should have a field in a table somewhere that includes all (4 in my case) of the options for the dropdown which is seperate to the actual saved_orig field. So I've done that and edited my code accordingly and whilst the dropdown is being populated correctly, it's not selecting the right option. table(1) name: combo_selected field name(containing selected item): selected table(2) name: combo_options field name(containing 4 options): options <?php $selected = mysql_query("SELECT selected FROM combo_selected"); $options = mysql_query("SELECT options FROM combo_options"); while ($nt = mysql_fetch_assoc($options)) { echo "<option value=\"$options\""; if ($options == $selected){ echo "selected";} echo">".$nt['options']."</option>"; } ?> I'm obviously missing something! Quote Link to comment https://forums.phpfreaks.com/topic/109576-solved-dropdown-box-populated-mysql-field-selected/#findComment-562076 Share on other sites More sharing options...
saint959 Posted June 10, 2008 Share Posted June 10, 2008 I think the below may work. from the off set it does not seem like you are actually getting the value from the combo_selected table. Also, im assuming the value in combo_selected is equal to one of the values in the combo_options table? <?php $selected_query = mysql_query("SELECT selected FROM combo_selected"); $row = mysql_fetch_assoc($selected_query) $selected = $row['selected']; $options = mysql_query("SELECT options FROM combo_options"); while ($nt = mysql_fetch_assoc($options)) { echo "<option value=\"$options\""; if ($options == $selected){ echo "selected";} echo">".$nt['options']."</option>"; } ?> Also, i think there is still going to be alot of work done here as you will have to get a way to link a unique record in the `combo_selected` table to specific user who is viewing the site (if i am thinking right about what it is you want). but i think that can come once you have the concept right. Sorry, but i leaving the office now and will only be back tomorrow. Quote Link to comment https://forums.phpfreaks.com/topic/109576-solved-dropdown-box-populated-mysql-field-selected/#findComment-562080 Share on other sites More sharing options...
New Coder Posted June 10, 2008 Share Posted June 10, 2008 This is similar to one of my pieces of code. try: $query = mysql_query("SELECT dropdown FROM combo") or die(mysql_error()); echo '<form action="action" method="post">'; echo '<select name="option_show">'; echo '<option>Choose Option</option>'; while($nt = mysql_fetch_array($query)) { echo '<option value="'.$nt[0].'"'.($nt[0]==$dropdown ? ' selected="selected"' : '').'>'.$nt[0].'</option>'; } echo '</select><br />'; echo '<input type="submit" value="Submit">'; echo '</form>'; You have the echo '<option>Choose Option</option>'; part just incase a record hasn't been selected previously. Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/109576-solved-dropdown-box-populated-mysql-field-selected/#findComment-562091 Share on other sites More sharing options...
ChrisDN Posted June 10, 2008 Author Share Posted June 10, 2008 No joy. I thought your suggestion had worked for a moment New Coder but it was only because of the ORDER BY you originally posted that put the relevant option to the top of the dropdown. I'll leave this for today and come back to it with a fresh pair of eyes tomorrow. Thanks for your suggestions today! Quote Link to comment https://forums.phpfreaks.com/topic/109576-solved-dropdown-box-populated-mysql-field-selected/#findComment-562122 Share on other sites More sharing options...
New Coder Posted June 10, 2008 Share Posted June 10, 2008 actually I have missed a section from my example. you will need to query to find orig option selected from the table you have stored the form data. like: $query1 = mysql_query("SELECT selected FROM combo_selected") or die(mysql_error()); $num = mysql_num_rows ($query1); if ($num !=0) { #retrieve data $row = mysql_fetch_array( $query1 ); $previous_option = $row["selected"]; } else { exit();} $query = mysql_query("SELECT dropdown FROM combo") or die(mysql_error()); echo '<form action="action" method="post">'; echo '<select name="option_show">'; echo '<option>Choose Option</option>'; while($nt = mysql_fetch_array($query)) { echo '<option value="'.$nt[0].'"'.($nt[0]==$previous_option ? ' selected="selected"' : '').'>'.$nt[0].'</option>'; } echo '</select><br />'; echo '<input type="submit" value="Submit">'; echo '</form>'; Quote Link to comment https://forums.phpfreaks.com/topic/109576-solved-dropdown-box-populated-mysql-field-selected/#findComment-562124 Share on other sites More sharing options...
ChrisDN Posted June 11, 2008 Author Share Posted June 11, 2008 That works absolutely perfectly! I'm gonna go read up on mysql_num_rows and then pick that apart to see exactly what it's doing. Many thanks for your help with that! Quote Link to comment https://forums.phpfreaks.com/topic/109576-solved-dropdown-box-populated-mysql-field-selected/#findComment-562976 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.