Jump to content

change sex type??


newb

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]
Link to comment
https://forums.phpfreaks.com/topic/23493-change-sex-type/
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].
Link to comment
https://forums.phpfreaks.com/topic/23493-change-sex-type/#findComment-106606
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]
Link to comment
https://forums.phpfreaks.com/topic/23493-change-sex-type/#findComment-106647
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??
Link to comment
https://forums.phpfreaks.com/topic/23493-change-sex-type/#findComment-107098
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]
Link to comment
https://forums.phpfreaks.com/topic/23493-change-sex-type/#findComment-107149
Share on other sites

Archived

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

×
×
  • Create New...

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.