newb Posted October 10, 2006 Share Posted October 10, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/23493-change-sex-type/ Share on other sites More sharing options...
newb Posted October 10, 2006 Author Share Posted October 10, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/23493-change-sex-type/#findComment-106600 Share on other sites More sharing options...
btherl Posted October 10, 2006 Share Posted October 10, 2006 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]. Quote Link to comment https://forums.phpfreaks.com/topic/23493-change-sex-type/#findComment-106606 Share on other sites More sharing options...
Daniel0 Posted October 10, 2006 Share Posted October 10, 2006 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 Link to comment https://forums.phpfreaks.com/topic/23493-change-sex-type/#findComment-106647 Share on other sites More sharing options...
newb Posted October 10, 2006 Author Share Posted October 10, 2006 [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?? Quote Link to comment https://forums.phpfreaks.com/topic/23493-change-sex-type/#findComment-107098 Share on other sites More sharing options...
GremlinP1R Posted October 10, 2006 Share Posted October 10, 2006 Okay stupid ? but what exactly do you whant? Quote Link to comment https://forums.phpfreaks.com/topic/23493-change-sex-type/#findComment-107148 Share on other sites More sharing options...
newb Posted October 10, 2006 Author Share Posted October 10, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/23493-change-sex-type/#findComment-107149 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.