Jump to content

Archived

This topic is now archived and is closed to further replies.

newb

change sex type??

Recommended Posts

i have this code for changing ur gender (if ur a male or female). it changes the value in the database but it doesnt display if ur chosen gender. help pleeze
[code]
<?php
$query = $config->query("SELECT * FROM $table_users WHERE username='$_SESSION[username]'");
$row = mysql_fetch_array($query);
$sex = array (
0 => 'Male',
1 => 'Female',
);

echo "<select name='sex'>";
if ( (in_array("$row[sex]", $sex)) ) {
$selected = "selected='selected'";
} else {
$selected = NULL;
}
$option = "
<option value='$sex[0]' $selected >$sex[0]</option>\n
<option value='$sex[1]' $selected >$sex[1]</option>\n";
echo $option;
echo "</select>";
?>
[/code]

Share this post


Link to post
Share on other sites
hmm, the html outputs this:
[code]
<select name='sex'>
<option value='Male' selected='selected' >Male</option>
<option value='Female' selected='selected' >Female</option>
</select>
[/code]
any help would be wonderful thankz

Share this post


Link to post
Share on other sites
The problem there is that you are setting $selected, then using the same value for both <option> tags.  To keep the same style as you have, you can create an array $selected[], and set

[code]$selected[0] = ' selected ';
$selected[1] = '';[/code]

for example, if you wanted 0 to be selected and 1 not selected.  Then where you set $option, you can use $selected[0] and $selected[1], just as you use $sex[0] and $sex[1].

Share this post


Link to post
Share on other sites
This will work: [code]<?php
$query = $config->query("SELECT * FROM $table_users WHERE username='$_SESSION[username]'");
$row = mysql_fetch_array($query);

$sex = array('Male','Female');
echo "<select name='sex'>\n";
foreach($sex as $sex)
{
if($sex == $row['sex'])
$selected = $sex==$row['sex'] ? " selected='selected'" : null;
echo "\t<option value='{$sex}'{$selected}>{$sex}</option>\n";
}
echo "</select>\n";
?>[/code]

Share this post


Link to post
Share on other sites
[quote author=Daniel0 link=topic=111021.msg449657#msg449657 date=1160458827]
This will work: [code]<?php
$query = $config->query("SELECT * FROM $table_users WHERE username='$_SESSION[username]'");
$row = mysql_fetch_array($query);

$sex = array('Male','Female');
echo "<select name='sex'>\n";
foreach($sex as $sex)
{
if($sex == $row['sex'])
$selected = $sex==$row['sex'] ? " selected='selected'" : null;
echo "\t<option value='{$sex}'{$selected}>{$sex}</option>\n";
}
echo "</select>\n";
?>[/code]
[/quote]

sigh, didnt work...still just outputted both value's as selected instead of the one the user has selected in the database..someone can help pls??

Share this post


Link to post
Share on other sites
quite obvious what i wanted if u read above.

nevermind figured it out on my own >_>...
[code]
<?php
$query = $config->query("SELECT * FROM $table_users WHERE username='$_SESSION[username]'");
$row = mysql_fetch_array($query);

$sex = array('Male','Female');
echo "<select name='sex'>\n";
foreach($sex as $sex)
{
if ( $sex == $row['sex'] ) {
$selected = 'selected="selected"';
} else {
$selected = NULL;
}
echo "\t<option value='{$sex}' {$selected}>{$sex}</option>\n";
}
echo "</select>\n";
?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.