"Picking" attributes

Permalink 3 users found helpful
Hi all,

I've created a multi-select attribute associated with a page, and I've sorted out how ot get all of the values by using getcollectionAttributeValue('xyz'), but it returns a bunch of stuff, most importantly, I'd like to pull all of the "value" items from this object...

here is my code:
$surroundings = $c->getCollectionAttributeValue('trail_surroundings');


Results in:
SelectAttributeTypeOptionList Object
(
[options:private] => Array
(
[0] => SelectAttributeTypeOption Object
(
[error] =>
[ID] => 1
[value] => Wooded
[displayOrder] => 0
)

[1] => SelectAttributeTypeOption Object
(
[error] =>
[ID] => 4
[value] => Rivers or Streams
[displayOrder] => 3
)

[2] => SelectAttributeTypeOption Object
(
[error] =>
[ID] => 5
[value] => Open Fields
[displayOrder] => 4
)

)

[error] =>
)

What I'd like to do is get all of the "value" values... How do I do this?

Thanks!

 
jordanlev replied on at Permalink Best Answer Reply
jordanlev
Try this:

$values = array();
foreach ($surroundings as $s) {
  $values[$s->getSelectAttributeOptionID()] = $s->getSelectAttributeOptionValue();
}


In your above example, the $values array should now look like this:
[1] => Wooded
[4] => Rovers or Streams
[5] => Open Fields


HTH!
MMoose replied on at Permalink Reply
That did it, awesome! THANKS!

I'll just have to read up on what those functions do. :)
hursey013 replied on at Permalink Reply
hursey013
This works great, however it seems that if a page with the multi select attribute only has one item in the multi select checked, it fails:

Fatal error: Call to a member function getSelectAttributeOptionValue() on a non-object

Any idea how to get around that?
jordanlev replied on at Permalink Reply
jordanlev
Sorry, I have no idea :(
You may want to post that as a new question to the forum -- I've noticed that once a few answers appear in a thread, they sometimes don't get re-read by people.
mbone99 replied on at Permalink Reply
If there is one only selected, you'll get a single SelectAttributeOptionValue object returned, but if there are multiples selected, you'll get an array of SelectAttributeOptionValue objects returned. Here's how we normalize:

$list = array();
$cats = $c->getAttribute('pageCategory');
if(method_exists($cats,'getSelectAttributeOptionValue')){
//only 1 is selected
     $list[] =$cats->getSelectAttributeOptionValue();
} else {
   foreach($cats as $cat){
        $list[] = $cat;
   }
}


Hope it helps -

mb
hursey013 replied on at Permalink Reply
hursey013
Very helpful, thank you.
hursey013 replied on at Permalink Reply
hursey013
Just out of curiosity, using your code above (which works great) is there any way to also determine the last record in the list? The reason I ask is because in a case I'm working with now, I just want to separate the different attributes with commas, but I dont want a comma to appear after the last record - know of any way around this? Thanks!
jordanlev replied on at Permalink Reply
jordanlev
This would probably be more easily accomplished using php's implode function (http://php.net/implode ), like so:
echo implode(',', $list);

If that doesn't work for your situation, though, you can get the last item like this:
$max = count($list)-1;
$last_item = $list[$max];
hursey013 replied on at Permalink Reply
hursey013
For one reason or another I can't wrap my head around this. All I want to do is display the list from the attribute separated by commas, however no matter what I do I still wind up with a trailing commna on the end. If anyone knows how I can get rid of that (implode doesn't appear to work) it would be greatly appreciated!
jordanlev replied on at Permalink Reply
jordanlev
implode() should not be adding a comma to the end. Can you post the code snippet you're using? Also can you do a "print_r($lists)" to see what the actual contents of that variable are?
Maybe there are extra empty elements at the end of the array so implode is concatenating empty strings and putting a comma between them?
hursey013 replied on at Permalink Reply
hursey013
Right now I am using mbone99's code exactly as it appears above. I've tried using implode instead of that code, but as you mentioned I don't think the data in $c->getAttribute('blah') is formatted properly to be able to work with that. Having a good solution to display the content of a multi select within a template would be huge for me, mbone99's code works great for making html lists of the data, I just cant get a solution working for commas though!
jordanlev replied on at Permalink Reply
jordanlev
Not sure I understand the full context here -- if you have a variable $list that is an array, then there are a bunch of different ways to inspect and manipulate it -- none of which has anything to do with concrete5 specifically, but rather just regular php.

So, for example, you could try putting this after that code sample above:
print_r($lists);
die();

and see what comes out of the browser (do a "View Source" to see it formatted properly). It should be showing you a plain old array, with what's inside it.
If the implode() function isn't working, perhaps it's because they aren't strings in the array? If it actually is giving you an array, and you want to identify the last element, you could try this:
$count=count($list);
$i=1;
foreach ($list as $item) {
  if ($i==$count) {
    $last_item = $item;
  }
  $i++;
}

or you could try this:
$last_item = end($list);


Hope that helps.
hursey013 replied on at Permalink Reply
hursey013
I have an multi-select attribute $c->getAttribute('speciality') which contains all the different specialties that a doctor practices. An example can currently be seen under the Dr's name on this page:

http://radltd.com/radiologists/matthew-l-bell-md/...

I am using mbone99's code above to accomplish this, but as you can see I can't use his code without it displaying a comma after the last record.

I don't care if I use the code I'm using now, or something else, I just want it to look exactly how it does now, only with no trailing comma. $5 paypal'ed right now to the best working code snippet to accomplish this. It also needs to work if only one option is selected in the multiselect attribute as mbone99's code does.
jordanlev replied on at Permalink Reply
jordanlev
mbone99's code only puts items into the list -- it does not output them. What is the code you're using to output the list?

And once again, it would be really helpful if you put this in:
print_r($list);
die();

and then hit the page, view source, copy that, and paste that here as well (then change it back so the site works).
hursey013 replied on at Permalink Reply
hursey013
Here's what I'm using now:

$list = array();
$cats = $c->getAttribute('specialty');
if(method_exists($cats,'getSelectAttributeOptionValue')){
    $list[] = $cats->getSelectAttributeOptionValue();
    echo $cats->getSelectAttributeOptionValue();
} else {
       foreach($cats as $cat){
           $list[] = $cat;
           echo $cat . ', ';
       }
}
hursey013 replied on at Permalink Reply
hursey013
Here is the print_r for $list:

Body Imaging, Nuclear Medicine, Breast Imaging, Array
(
    [0] => SelectAttributeTypeOption Object
        (
            [error] => 
            [ID] => 3
            [value] => Body Imaging
            [displayOrder] => 2
        )
    [1] => SelectAttributeTypeOption Object
        (
            [error] => 
            [ID] => 5
            [value] => Nuclear Medicine
            [displayOrder] => 4
jordanlev replied on at Permalink Reply
jordanlev
(Sorry about the last comment [now deleted] -- I didn't see that you posted the output code)

This should work:
$list = array();
$cats = $c->getAttribute('specialty');
if(method_exists($cats,'getSelectAttributeOptionValue')){
    $list[] = $cats->getSelectAttributeOptionValue()->value;
} else {
       foreach($cats as $cat){
           $list[] = $cat->value;
       }
}
echo implode(',', $list);
hursey013 replied on at Permalink Reply
hursey013
That did it, I really appreciate it. I'm hardly a developer and I often get stuck on little things like this and bang my head for hours - I should read a php book one of these days...send me your paypal account and I'll send $5 your way. Thanks again.
jordanlev replied on at Permalink Reply
jordanlev
Don't be too hard on yourself -- this is how people learn (by banging their head against the wall, not from a book). And you're in luck, because I don't have a paypal account so you can keep the $5 for yourself!
jordanlev replied on at Permalink Reply
jordanlev
BTW, that's a really nice site.
hursey013 replied on at Permalink Reply
hursey013
Thanks, and again I appreciate the assistance. Get yourself a paypal account too.