Jump to content

Option fieldtype and select status


Juergen
 Share

Recommended Posts

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...