Re: selected value from database

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If the option values are filled via a query, John's code can be simplified a
bit by putting it within the foreach statement.

if( $level==$currentLevel ) echo \"selected\";

Edward Dudlik
Becoming Digital
www.becomingdigital.com


----- Original Message -----
From: "John W. Holmes" <holmes072000@charter.net>
To: "'Peter Gumbrell'" <peter@orillia.net>; <php-db@lists.php.net>
Sent: Friday, 18 April 2003 20:30
Subject: RE:  selected value from database


> In an update form, I have a select list with only 3 values: admin,
> author and contributor. How do I have the value currently held in the
> database be the one which is selected? Here is the code that I have so
> far;
>
> print "<td> Level: <select name=\"level\" value=\"$level\"><option
> value=\"admin\">Admin</option>\n";
> print "<option value=\"author\">Author</option><option
> value=\"contributor\">Contributor</option></select></td></tr>\n";

You don't set a 'value' attribute for a <select> element. You place
'selected' into the <option> element that you want selected.

So, let's think about how to do that. If $level is 'Admin' you want to
display 'selected' in that <option>, right? Same for the rest of the
<option> values.

So, the basic way to do it is:

<option value="Admin"<? If($level=='Admin') echo ' selected';?>>

and continue that for each option.

There are other methods to do it, but this is the basic method. You can
place all of the possible values into an array and loop through it,
checking for $level == $value on each loop, too. This is more suitable
for larger <select> elements.

---John W. Holmes...

PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux