Page 1 of 1

get mysql value to dispaly in drop menu

Posted: Sat Jul 07, 2007 6:03 pm
by gregorious
I am building a CMS for a real estate site, with COMMERCIAL and RESIDENTIAL page listings. The CMS has ADD/SELECT/EDIT/DELETE pages for both sections. The ADD page is a basic form page, with a coulple of DROP MENUs; when submitted the data is recored in a MYSQL database.

The EDIT page is a mirror of the ADD page; the data is retrieved from the database and placed into the form fields for editing by the user - except for the DROP DOWN MENUs. The drop menus default to the first value in the form but I can't seem to get the DROP MENUs on the EDIT page to show the value that is in the database.

Code: Select all

<select name="restype" class="">
	         	<option selected value="">  
        		<option value="SINGLE FAMILY HOME">SINGLE FAMILY HOME
	        	<option value="DUPLEX FAMILY HOME">DUPLEX FAMILY HOME
	        	<option value="MULTI FAMILY PROPERTY">MULTI FAMILY PROPERTY
	        	<option value="CONDOMINIUM">CONDOMINIUM
	        	<option value="LAND (RESIDENTIAL)">LAND (RESIDENTIAL)
	</select>
Been working with PHP MYSQL for less than a year, so my apolgy if this is "no brainer".

Thanks in advance,
Greg

Posted: Sat Jul 07, 2007 6:22 pm
by superdezign
When displaying the options, you'd have to check each value against the one in the database, and echo "selected" on the option.

Success!!

Posted: Sun Jul 08, 2007 4:28 am
by gregorious
Thanks, I was able to resolve this issue (with help), and the solution is posted below for other newbies who are looking for the same solution.

Code: Select all

<select name="restype" class="listing01">
<option selected value="<?php echo $restype; ?>"><?php echo $restype; ?></option>
<option value="SINGLE FAMILY HOME">SINGLE FAMILY HOME</option>
<option value="DUPLEX FAMILY HOME">DUPLEX FAMILY HOME</option>
<option value="MULTI FAMILY PROPERTY">MULTI FAMILY PROPERTY</option>
<option value="CONDOMINIUM">CONDOMINIUM</option>
<option value="LAND (RESIDENTIAL)">LAND (RESIDENTIAL)</option>
</select>

Posted: Sun Jul 08, 2007 6:28 am
by miro_igov
Ummm, is this not causing some items displayed twice? Not profy.

Posted: Sun Jul 08, 2007 7:07 am
by superdezign
That solution doesn't make much sense.

Posted: Tue Jul 10, 2007 12:08 pm
by gregorious
Ummm, is this not causing some items displayed twice? Not profy.
That solution doesn't make much sense.

yeah, the variable $restype does show up twice in the drop down menu - once as the selected choice and again in the menu as a regular choice. my client is not very computer literate so I am interested in any other solution to the one above.

Learning keeps me young.

Greg

Posted: Tue Jul 10, 2007 2:55 pm
by RobertGonzalez
Your solution is not a solution at all. What you want to do is, as you build the drop down menu, check each item in the menu to see if it is the same as what is know from the database. If it is, echo ' selected="selected"', otherwise echo nothing.