Jump to content


Photo

change sex type??


  • Please log in to reply
6 replies to this topic

#1 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 10 October 2006 - 02:26 AM

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
<?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>";
?>


#2 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 10 October 2006 - 02:28 AM

hmm, the html outputs this:
<select name='sex'>
<option value='Male' selected='selected' >Male</option>
<option value='Female' selected='selected' >Female</option>
</select>
any help would be wonderful thankz

#3 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 10 October 2006 - 03:01 AM

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

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

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].

#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 10 October 2006 - 05:40 AM

This will work:
<?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";
?>


#5 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 10 October 2006 - 07:28 PM

This will work:

<?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";
?>


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??

#6 GremlinP1R

GremlinP1R
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationSouth Africa

Posted 10 October 2006 - 09:16 PM

Okay stupid ? but what exactly do you whant?

#7 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 10 October 2006 - 09:21 PM

quite obvious what i wanted if u read above.

nevermind figured it out on my own >_>...
<?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";
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users