tippy_102 Posted March 18, 2007 Share Posted March 18, 2007 I want to create a dynamic drop down option list in a form, but I just can't wrap my head around this. Both of the below scripts work as intended, but how do I make the mySql query results work with my existing script? Yes, I know I must replace the "$options = array" part in the top piece of code, but I need a little more guidance than that. <?php function select_menu($name, $options, $selected) { $list = ''; foreach ($options as $value => $text) { $is_selected = $value == $selected ? ' selected="selected"' : ''; $list .= " <option value=\"$value\"$is_selected>$text</option>\n"; } return "<select name=\"$name\" id=\"$name\">\n$list</select>\n"; } echo "<tr><td>size:</td><td>"; $options = array( '0' => 'select an option', '1' => 'Micro', '2' => 'Small', '3' => 'Regular', '4' => 'Large' ); $menu_name = 'size'; // grab selected_val from db $selected_value = $size; echo select_menu($menu_name, $options, $selected_value); echo "</td></tr>"; ?> <?php $query = "SELECT * FROM topic_size"; $result = mysql_query($query) or die(mysql_error()); while($options = mysql_fetch_array($result)) { echo $options['topID']. $options['topicName']; echo "<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/43268-solved-dynamic-form-option-list/ Share on other sites More sharing options...
Barand Posted March 18, 2007 Share Posted March 18, 2007 Very similar to array version function selectMenu ($name, $selected) { $query = "SELECT topID, topicName FROM topic_size"; $result = mysql_query($query) or die(mysql_error()); echo "<select name='$name'><option value='0'>select an option</option>"; while(list($value, $text) = mysql_fetch_row($result)) { $sel = $value=$selected ? 'selected' : ''; echo "<option value='$value' $sel> $text</option>"; } echo '</select>'; } Quote Link to comment https://forums.phpfreaks.com/topic/43268-solved-dynamic-form-option-list/#findComment-210123 Share on other sites More sharing options...
tippy_102 Posted March 20, 2007 Author Share Posted March 20, 2007 That's perfect! Thank you very much Barand ! Quote Link to comment https://forums.phpfreaks.com/topic/43268-solved-dynamic-form-option-list/#findComment-210887 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.