|
-
Oct 4th, 2007, 08:04 AM
#1
Select Menu issue
hai friends,
my select Menu as follows
Code:
<select name="cmbgender" id="cmbgender" >
<option>Male</option>
<option>Female</option>
</select>
When the user attempt to edit a record, i load the data relavent to the record from the db in to a form where above control is a part of it.
Now i need to select Male if the value in the db is Male, else Female.
How do i do that ?
Assume, $gender is the variable which holds the Male / Female data retrived.
-
Oct 4th, 2007, 12:11 PM
#2
Fanatic Member
Re: Select Menu issue
This query should work for most (some) DB servers.
Code:
"SELECT * FROM people WHERE gender = '" . $gender . "'"
-
Oct 4th, 2007, 02:02 PM
#3
Re: Select Menu issue
brother k1II3dr3rd4gorOn
Thanks. but my question is how to select an existing item in the list
-
Oct 4th, 2007, 02:18 PM
#4
Fanatic Member
Re: Select Menu issue
 Originally Posted by Fazi
brother k1II3dr3rd4gorOn
Thanks. but my question is how to select an existing item in the list 
Prehaps something like:
Code:
if ($gender == "male") {
$menu = '<select name="cmbgender" id="cmbgender" >
<option value="male">Male</option>
<option value="female">Female</option>
</select>';
} else {
$menu = '<select name="cmbgender" id="cmbgender" >
<option value="female">Female</option>
<option value="male">Male</option>
</select>';
}
-
Oct 4th, 2007, 02:24 PM
#5
Re: Select Menu issue
Hai K1,
i just got a reply from an out side forum.
Code:
<option <?php if ($gender=="Male") { ?> selected="selected" <?php }?> >Male</option>

I thing your method in #4 will have a large over head when it come to a big list
-
Oct 4th, 2007, 04:27 PM
#6
Re: Select Menu issue
Yeah, the way he did it would be ok for 2 items, but for multiple it would be best to go through a loop.
My usual boring signature: Something
-
Oct 4th, 2007, 05:22 PM
#7
Re: Select Menu issue
Guys, Do you undestind this
Code:
<option <?php if ($gender=="Male") { ?> selected="selected" <?php }?> >Male</option>
I have some confusion and explanation would be much appriciated.
-
Oct 4th, 2007, 05:43 PM
#8
Re: Select Menu issue
well it is pretty straight forward...
the php is saying:
if the gender is male, then echo "selected='selected'"
so you would need this:
PHP Code:
<select>
//so if the person is male, this one will be selected
<option value='male' <?PHP if($gener=="male") { ?> selected='selected <?PHP } ?>>
// if it is a female, this will be seleced
<option value='female' <?PHP if($gener=="female") { ?> selected='selected <?PHP } ?>>
</select>
My usual boring signature: Something
-
Oct 4th, 2007, 05:52 PM
#9
Re: Select Menu issue
still i have confusing on the open and close tags,
why this is imediatly colose?
Code:
<?PHP if($gener=="male") { ?>
so what will happen to the following html?
Last edited by Fazi; Oct 4th, 2007 at 07:36 PM.
-
Oct 4th, 2007, 06:19 PM
#10
Re: Select Menu issue
well that is the correct way to show html with php.
another (incorrect) way to show it would be:
<?PHP if ($gener=="male") { echo "selected='selected'" } ?>
My usual boring signature: Something
-
Oct 4th, 2007, 06:57 PM
#11
Re: Select Menu issue
Thanks dclamp for the info.
-
Oct 4th, 2007, 07:24 PM
#12
Re: Select Menu issue
no problem If your problem is solved, set it as resolved and give rep accordingly
My usual boring signature: Something
-
Oct 7th, 2007, 02:38 AM
#13
Re: Select Menu issue
It's <?php, not <?PHP.
Also, you need to specify the value attribute on each option.
An alternative way of doing it:
PHP Code:
<?php
$s = ' selected="selected"';
$sel = $gender == 'male' ? array($s, '') : array('', $s);
?>
<select name="cmbgender" id="cmbgender">
<option value="male"<?php echo $sel[0] ?>>Male</option>
<option value="male"<?php echo $sel[1] ?>>Female</option>
</select>
This is overkill in this particular case, but preferable if the comparison is complex or the list of options is long, since it is only evaluated once per set of options instead of once per option.
Last edited by penagate; Oct 7th, 2007 at 02:41 AM.
-
Oct 7th, 2007, 11:30 AM
#14
Re: Select Menu issue
@pena: why is <?PHP not a correct way to open php tags?
My usual boring signature: Something
-
Oct 7th, 2007, 06:25 PM
#15
Fanatic Member
Re: Select Menu issue
 Originally Posted by dclamp
@pena: why is <?PHP not a correct way to open php tags?
http://www.php.net/manual/en/language.basic-syntax.php
All the ways the show it should be php not PHP.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|