smc Posted January 27, 2007 Share Posted January 27, 2007 Hey,I have a drop down menu avaiable for editing and submitting articles in my CMS. My drop down menu on the HTML page has 3 entries, how can I get it to automatically choose the entry in the database for editing purpoes.For example: I have Apples in my database under fruit. In editing the food favorites I have a list with Apples, Oranges, and Pears. I want to choose Apples automatically.Using the above example, I tried value="<?php echo fruit; ?>" but no dice.Any ideas?Thanks! Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 28, 2007 Share Posted January 28, 2007 to make an <option> in a <select> be automatically chosen, add selected to it.So:<option value="apples" selected>Apples</option> Quote Link to comment Share on other sites More sharing options...
smc Posted January 28, 2007 Author Share Posted January 28, 2007 But how can I make that dynamic instead of adding an entire series of variables with if statements? :P Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted January 28, 2007 Share Posted January 28, 2007 [code]<?php echo "<form method=\"post\" action=\"". $_SERVER['php_self'] ."\">\n"; echo "<select name=\"dropdown\">\n"; $sql = "SELECT * FROM your_table"; $query = mysql_query($sql); while($row = mysql_fetch_array($query)){ echo "<option name=\"name\" value=\"". $row['value'] ."\"". (($row['value'] == $_POST['dropdown']) ? " SELECTED" : "") ."\">\n"; } echo "</select>\n"; echo "<input type=\"submit\" value=\"Submit\">\n"; echo "</form>\n";?>[/code]untested. Quote Link to comment Share on other sites More sharing options...
smc Posted January 28, 2007 Author Share Posted January 28, 2007 Right and that would dynamically create an entry but then there would be a duplicate in the drop down menu somewhere Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted January 28, 2007 Share Posted January 28, 2007 [quote author=smc link=topic=124351.msg515154#msg515154 date=1169951565]Right and that would dynamically create an entry but then there would be a duplicate in the drop down menu somewhere[/quote]a duplicate??? what do you mean? Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted January 28, 2007 Share Posted January 28, 2007 I don't know how you have your database set up but you could just use PHP to echo out your options, something like a[code]<?phpwhile($row[variable] = mysql_fetch_array($var_result)) {if ($something == $somethingelse) {$selected = "selected=\"selected\" " ;}echo("<option name=\"name\" value=\"value\" $selected/>");reset($selected);}?>[/code]Like I said I don't know how your whole script is set up so that's a very simplified version but sometimes the simplified ways help figure out the complicated ones ^_^ (at least for me that's true). This is a similar way I did dynamic drop down for a project.Also if you are not using XHTML then the $selected = 'selected'Hope this helps, sorry if it doesn't ^_^ Quote Link to comment Share on other sites More sharing options...
Tripic Posted January 28, 2007 Share Posted January 28, 2007 How are you aproaching this is it all on one page ie you select apple from the drop down box and it populates the feilds so you can edit and update or do you have a list some place with the items where you click on the item name ie lick on apples and it brings you to a page to edit apples. Im just a bit lost here is your problem populating the drop down box or poulating the fields after you have chosen what you want to edit Quote Link to comment Share on other sites More sharing options...
tippy_102 Posted January 28, 2007 Share Posted January 28, 2007 If this what you are looking for?[code]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 "Size:"; $options = array( 'value for this option' => 'text displayed for this option', '1' => 'Small', '2' => 'Medium', '3' => 'Large',); $menu_name = 'ud_Size'; // grab selected_val from db $selected_value = $Size; echo select_menu($menu_name, $options, $selected_value); [/code] Quote Link to comment 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.