Jump to content

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/45784-dynamic-drop-down-menu/
Share on other sites

<?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";
?>

  • 2 months later...

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.

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;	
			                        }
					}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.