Jump to content

How do I get the title of a SelectOptions option, by its option id?


desbest
 Share

Recommended Posts

How do I get the title of a SelectOptions option, by its option id?

I have this code.

$options = $fieldtypes->get('FieldtypeOptions')->getOptions("experienced_in");

I do not know what to add after it and I've searched google and the processwire forum and couldn't find anything.

 

Link to comment
Share on other sites

With your code:

$options = $fieldtypes->get('FieldtypeOptions')->getOptions("experienced_in")->title;

Much shorter and probably much easier in page context:

$optionsId = $page->optionsfieldname; // the ID

$optionsValue = $page->optionsfieldname->value; // the value

$optionsTitle = $page->optionsfieldname->title; // the title

 

  • Like 1
Link to comment
Share on other sites

Yes but I'm not getting the title from the current page, so I can't use the $page ones.

How do I change 

$options = $fieldtypes->get('FieldtypeOptions')->getOptions("experienced_in")->title;

to choose the ID I want, as there are multiple options and it keeps getting the first one. I want to choose a number of one of the array entries.

I tried to add [0] or [1] to the line of code above, and it didn't work.

Link to comment
Share on other sites

Try this:

$options = $fieldtypes->get('FieldtypeOptions')->getOptions("optionsfield")->get('id=2')->title;

Easier to maintain and for further use:

$options = $fieldtypes->get('FieldtypeOptions')->getOptions("optionsfield");
$options->get("id=$yourId")->title;

 

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