Version 8.1.0 getAttribute from user
Permalink
I am trying to get an array of user attributes using the following code:
<?php
$u = new User();
if($u->isLoggedIn()) {
$ui = UserInfo::getByID($u->getUserID());
echo $ui->getAttribute('attribute_handle');
}
?>
However the attribute I am getting has the option of multiple selects and therefore prints the out like "Attribute 1 Attribute 2" instead of an array which can be accessed. Is there a way to get the response as an array if more than one option is selected for a multi-select attribute?
<?php
$u = new User();
if($u->isLoggedIn()) {
$ui = UserInfo::getByID($u->getUserID());
echo $ui->getAttribute('attribute_handle');
}
?>
However the attribute I am getting has the option of multiple selects and therefore prints the out like "Attribute 1 Attribute 2" instead of an array which can be accessed. Is there a way to get the response as an array if more than one option is selected for a multi-select attribute?
You can do this
Thanks for responding!
Any clue why I would be getting
Invalid argument supplied for foreach()
I did a gettype() on the returned element from getAttribute() and it comes back as "object" but running the code you supplied doesn't seem to work.
Any clue why I would be getting
Invalid argument supplied for foreach()
I did a gettype() on the returned element from getAttribute() and it comes back as "object" but running the code you supplied doesn't seem to work.
What type of attribute are you dealing with? Is it a Select attribute with Multiple values or is it something else?
Yeh its a select attribute with multiples values (as in user can select more than one and there is going to be a fairly large list).
Then I have no idea, I have a select attribute with the handle 'multi_select_test' and this code outputs fine for me
$u = new User(); if($u->isLoggedIn()) { $ui = UserInfo::getByID($u->getUserID()); $attributeValues = $ui->getAttribute('multi_select_test'); foreach($attributeValues as $selectOptionValue){ echo $selectOptionValue->getSelectAttributeOptionDisplayValue().'<br /><br />'; } }
Strange, I am copying your example exactly, but changing the attribute handle and it keeps coming up saying the same Invalid argument supplied for foreach()
I think I have found out why.
I added some logic in to detect if an empty string or empty object was being passed to the foreach. I think it was an empty object that caused it as it was expecting some values, which on one login I was getting and another I wasn't causing the error on one user!
I added some logic in to detect if an empty string or empty object was being passed to the foreach. I think it was an empty object that caused it as it was expecting some values, which on one login I was getting and another I wasn't causing the error on one user!