chrisuk Posted April 5, 2007 Share Posted April 5, 2007 I have a customer table and I wish to be able to dynamically generate a drop down menu, to, for example, select the customer for which operations are to be performed. after I have made the initial select statements, I assume i will then need to use a while loop but this is where I am getting stuck....I am familiar with using while loops for creating tables dynamically but i'm not sure about drop downs. I just wish to show "company name" in the menu. While it be something along the lines of.... while ( $row = mysql_fetch_array ( $sql ) option = $customer_name etc.. ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/45784-dynamic-drop-down-menu/ Share on other sites More sharing options...
boo_lolly Posted April 5, 2007 Share Posted April 5, 2007 <?php echo "<select name=\"dropdown\">\n"; while($row = mysql_fetch_array($query)){ echo "<option value=\"{$row['column']}\""; (($row['column'] == $_POST['dropdown']) ? (" SELECTED") : ("")); echo ">{$row['column']}\n"; } echo "</select>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/45784-dynamic-drop-down-menu/#findComment-222428 Share on other sites More sharing options...
chrisuk Posted April 6, 2007 Author Share Posted April 6, 2007 that's great! knew I had the rough logic in my mind! thanks Quote Link to comment https://forums.phpfreaks.com/topic/45784-dynamic-drop-down-menu/#findComment-222958 Share on other sites More sharing options...
chrisuk Posted June 21, 2007 Author Share Posted June 21, 2007 Just to take this one step further... how would I go about making whatever option is selected sticky? ie so that when the form is reloaded, the last saved menu option will be what appears in the menu? (as opposed to a static "selected" menu item) thanks Quote Link to comment https://forums.phpfreaks.com/topic/45784-dynamic-drop-down-menu/#findComment-279269 Share on other sites More sharing options...
thefortrees Posted June 21, 2007 Share Posted June 21, 2007 I am trying to figure out this as well... Yesterday, to solve that problem, I created a loop that would be the last selected option (or value from DB) first in the list. The only problem with my loop is that it leaves one value out.. I'm not sure why, still working on that one. Here is my code - $temp = array(); $val = array_search($row['value'], $array); $temp[] = $array[$val]; $count = 0; echo "<td><select name='lob'>"; foreach ($array as $value){ if ($value == $row['value']){ continue; } else{ $temp[] = $value; echo "<option value='" . $temp[$count] . "'>" . $temp[$count]; } $count++; } What this code does is take the values from an $array that holds previous values (ie - the values in the drop down list or values stored in the db). It then finds the desired value to be first in the drop down list with array_search, then puts it first in temp[]. Once in the loop, if the current value in $array == the value I ALREADY stored in $temp[], then it skips over it and starts at the top of the loop. Like I said, it leaves one element out, so it doesn't work perfectly yet, but it does put the desired value first. Quote Link to comment https://forums.phpfreaks.com/topic/45784-dynamic-drop-down-menu/#findComment-279303 Share on other sites More sharing options...
thefortrees Posted June 21, 2007 Share Posted June 21, 2007 Scratch that - I spent all that time doing that loop. All the select option needs is selected='selected', so the code will probably look like this to pre-select a value: echo "<td><select name='lob'>"; foreach ($array as $value){ if ($value == $row['value']){ echo "option value='" . $value . "' checked='checked' } else{ echo "<option value='" . $value . "' selected='selected'>" . $value; } } Quote Link to comment https://forums.phpfreaks.com/topic/45784-dynamic-drop-down-menu/#findComment-279316 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.