Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate a drop down with selected value and others

Status
Not open for further replies.

Bravogolf

Programmer
Nov 29, 2002
204
GB
Hi genii :)

When I retrieve the status of a customer, I want to have a drop down for the value of one particular column (which will only ever contain Sold, Rejected or Outstanding).

How can I specify in the drop down menu to make the current value the selected value?

So if the customers status is Sold, how can I can create a drop down menu that default to Sold as being selected within the drop down and Oustanding and Rejected as the other two available options?
 
The question is how is that information stored so far? Say you're retrieving it from a database where 0 is sold, 1 is rejected and 2 is outstanding. And say you're looping through recordset and reading those values. I would then put these values in an array and for every item iterate through that array. While iterating, I would check if the value in the array matches the value in the database. If it does, output [tt]selected="selected"[/tt] at the end of opening <option> tag. Hope that makes sense.
 
Sorry dude, my question is a bit lacking, I admit :)

I'm retreiving an individual record from a customer (from a MySQL database) with PHP. Amongst other columns is one called status. I want to be able to change the status of that customer via a drop down menu.

The three possible values, as they appear within the table, are in varchar format and appear as "Sold", "Rejected" or "Outstanding".

What I have already is a form for the user to change the details. I would now like is a drop down menu with the three values "Sold", "Rejected" or "Outstanding" in it, but would like the value currently assigned to that particular record to be selected.

:)
 
So (in pseudo code):

> loop through all options in the drop down (3 of them)
> test to see if the one you are examining has the same value as the record you retrieved
> if it is, then add selected="selected" to the <option> tag

You would do this server-side (of course).

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Yes, exactly :)

Erm ... forgive me if I'm pushing my luck a little here, but how?
 
Code:
$query="SELECT * from artisttable order by arname";
$q_result=mysql_query($query,$conn);
$templist='';
$templist = "<select name=\"agarid\"><option value=\"\">Empty";
while ($ary_stage = mysql_fetch_assoc($q_result)) { //one status
  while (list($key,$val) = each($ary_stage)) { $$key = $val; }

	   $templist .= "<option value='" . $arid . "'";
		if ($arid==$agarid) {$templist .= " SELECTED ";}	
	   $templist .= ">" . $arname;
} //end one stage
$templist .=  "</select>";
$artistlist=$templist;

here is a sample code I use for my drop down lists. ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top