Dropdowns, selected and editing documents

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
freefall
Forum Commoner
Posts: 48
Joined: Sun Jun 18, 2006 3:55 am

Dropdowns, selected and editing documents

Post by freefall »

Hi, I've been curious about this for ages but how do you go about ensuring your select list has the correct value marked as selected when updating a forum.

Say for example I have made a form with a month field on it as a select list. The user selects the month, saves the document (form) to mysql. Later that day they then decide to edit the data. When they go into the document (form) the values have obviously been prepopulated from the db including the month being marked as SELECTED (ie the corresponding value stored in the db).

This might be the simplest question answer known to man I dont know, but I've written 3 complex bits of code to resolve it. One of which I'm 50% happy with, the other 2 I wanna burn.

Any ideas?

ps hi all btw joined today, been reading for hours, great site.
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

Compare the month returned by the DB to the select list, act on the results:

Code: Select all

if($monthToSelect == $monthPrePopulated)
	{
		echo "<option value=\"$monthToSelect\" selected=\"selected\">$monthToSelect</option>\n";
	}
		else
	{
		echo "<option value=\"$monthToSelect\">$monthToSelect</option>\n";
	}
freefall
Forum Commoner
Posts: 48
Joined: Sun Jun 18, 2006 3:55 am

Post by freefall »

2 things with that

a) if you store that list of variables in a relational db you will store the id not text

b) I dont want to store every list in the db.
Post Reply