Juergen Posted April 15, 2015 Share Posted April 15, 2015 Hello at all, I have added a select field with the option field type where the user can select the gender (mister or miss) to the user template. On the frontend the user has a profile page with a form where he can update his user data (if he/she is logged in). On the frontend form I call the gender option field in this way: //grab stored gender from DB $storedgender = $user->usergender; //make select for gender options $genderoptions = $fieldtypes->get('FieldtypeOptions')->getOptions('usergender'); foreach($genderoptions as $option) { $genderid = $option->id; $gendervalue = $option->$value; $gendertitle = $option->$title; $genderselected = ""; if ($storedgender == $genderid) { $genderstatus = " selected"; } $genderoptions .= "<option$genderselected value='$option->id'>$option->title</option>"; }; //here you can output the option tags wherever you want (in my case in the user frontend form) echo $genderoptions; This renders the output in the way that I want but..... Problem: The comparison between the stored user gender (e.g. mister with the value 1) and the genderstatus "selected" doesnt work and I dont know why $genderselected = ""; if ($storedgender == $genderid) { $genderstatus = " selected"; } //this kind of comparison doesnt set the selected status correctly It always put the status "selected" to the first option in the foreach statement independent of the stored user gender. I cant figure out what the problem could be ? Has anyone an idea? I've been trying for some time to find a solution, but I can not find one. Best regards Jürgen Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 15, 2015 Share Posted April 15, 2015 $user->usergender is not the id of the selected field it's the label. Just like you would get the page from a pagefield and not the id. This will get you the id. $user->getUnforamatted("usergender"); 2 Link to comment Share on other sites More sharing options...
Juergen Posted April 15, 2015 Author Share Posted April 15, 2015 Yes that was the problem (Hint: This code is for multilingual site): You have to insert the following code to get the values in different languages: if ($user->language->name != 'default') { $title = "title{$user->language}"; $value = "value{$user->language}"; } else { $title = 'title'; $value = 'value'; } Then you can start to create the options for the select $storedgender = $user->YOUROPTIONFIELDNAME->id; $genderoptionstag = ""; //make select for gender options $genderoptions = $fieldtypes->get('FieldtypeOptions')->getOptions('YOUROPTIONFIELDNAME'); foreach($genderoptions as $option) { $genderid = $option->id; $gendervalue = $option->$value; $gendertitle = $option->$title; if ($storedgender == $genderid) { $genderstatus = " selected"; } else { $genderstatus = ""; } $genderoptionstag .= "<option$genderstatus value='$genderid'>$gendertitle</option>"; }; //here you can output the option tags wherever you want (in my case in the user frontend form) echo $genderoptionstag; Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now