Multi Select Attribute problem

Permalink 1 user found helpful
I have a page with a multi select attribute with allowing mulit chooses. In my code i retrieve the values with this:
$ProjectGroup = $cobj->getCollectionAttributeValue('ProjectGroup');

if only one item is check i can check it with this code
if ($ProjectGroup == "RapidCoolRooms")


but if i have multi items check this does not work. Need to see if "RapidCoolRooms" has been selected?

Frank

wizardontherun
 
ijessup replied on at Permalink Reply
ijessup
If multiple items are selected I think it will look more like: RapidCoolRooms\nOtherItems\nAnotherItem\n

Where \n is PHP's new line phrase

You need to do something along the lines of:
if(strstr('RapidCoolRooms', $cobj->getCollectionAttributeValue('ProjeectGroup')) {
// Do Something
}
87up replied on at Permalink Reply
87up
Hello ijessup,

i am having the same problem. i tried your code, but it doesn't work.

i have changed my select attribute to allow multiple options to be chosen.
When 2 or more options are selected at the same time, only the first selected option is working. How do I make my filter check for all the selected options. ?

Here is my code:
$values = explode("\n", $cobj->getCollectionAttributeValue('kompetencer')->current()->value);
foreach ($values as $value) {
              $myValues[] = $value;
           echo $value;
         };
if($value == $c->getCollectionName())


Thank you in advance.
SVijay replied on at Permalink Reply
SVijay
Hi,

Try this, it may help
You need to implode the values while storing in database and then explode while checking and viewing.

OR

Just use foreach and If statement within the foreach to check the values.

Hope you understand
87up replied on at Permalink Reply
87up
Hello OpenJuiceVijay,

Thank you for your answer.

That might help me solve my problem. :)
jbx replied on at Permalink Reply
jbx
Hey 87up - did you get my response in the Services forum? There's some example code and a full explanation in there...

Jon
wizardontherun replied on at Permalink Reply
wizardontherun
do you have a link to this?
jbx replied on at Permalink Reply
jbx
I posted it in the Service Partners forum, but I'll copy it below for reference:

Try this:

foreach ($cobj->getCollectionAttributeValue('kompetencer') as $option):
  if ($option == $c->getCollectionName()):
    // do stuff...
    break;
  endif;
endforeach;


As a way of explanation:

$cobj->getCollectionAttributeValue('myValue') returns an object of all selected options with several methods to traverse through them. '->current()' returns the current selected option, which will always be the first selected option, unless you have used a method like '->next()' or '->rewind()'. There is also a '->contains()' method, but I haven't checked what needs to be passed into that yet.

Jon
87up replied on at Permalink Reply
87up
Hello jbs,

Yes I got your response in the Service Partners forum. It works like a charm.. Thank you for that. :)