Filter userlist by address attribute

Permalink
Hi
Is it possible to filter a userlist by the city filed of the address attribute?
I've tried
$ul->filterByAttribute('location', "London", "LIKE");
where 'location' is the handle of the address attribute
and
$ul->filterByAttribute('ak_city', "London", "LIKE");\
but neither work.
Many thanks

rosie607
View Replies: View Best Answer
zuna replied on at Permalink Reply
This works for me:
Maybe with some changes this works for you:
$city='London';
$ul->filterByAttribute('ak_city',"%\n$city\n%",'like');
Usually i use this but not in address attribute:
$ul->filterByAttribute('location',"%\n$city\n%",'like');
rosie607 replied on at Permalink Reply
rosie607
Hi Zuna

Thanks but this is what I was already doing.

When I use
$city='London';
$ul->filterByAttribute('ak_city',"%\n$city\n%",'like');

or
$ul->filterByAttribute('ak_city',"London",'like');


I get the error message
An unexpected error occurred.
Call to a member function getController() on a non-object
JohntheFish replied on at Permalink Reply
JohntheFish
The address attribute is a single attribute, city is just a component of it, so I don't think it will have a key of its own.

You will need whatever handle you have given the overall address attribute, prefixed by ak_ .

The catch is that you could also get a positive result for things like "123 London Road, Brighton, ...", unless you are very careful about the match expression.
rosie607 replied on at Permalink Reply
rosie607
Hi John

thanks for the reply I'm still getting an error

When I use

$ul->filterByAttribute('ak_location', "London", "LIKE");


where 'location' is the handle of the address attribute

I get the error

An unexpected error occurred.
Call to a member function getController() on a non-object
JohntheFish replied on at Permalink Reply
JohntheFish
User search uses the table UserSearchIndexAttributes. That will have the correct form of your attribute handle as a column heading.

Also check you have enabled searching for the attribute.
rosie607 replied on at Permalink Reply
rosie607
Hi John

I looked in the table for the handle and it says it's ak_location_city
I have tried
$ul->filterByAttribute('ak_location_city', "London", "LIKE");

I'm still getting the same error as before
I also set the attribute in Members > Attributes > Location
Content included in user keyword search.
Field available in Dashboard User Search.

Is there any thing else I need to do?
Thanks
JohntheFish replied on at Permalink Reply
JohntheFish
The good point there is that you now know what the attribute is named and that it has been split up in the search data.

I am working off the top of my head, I don't have the code in front of me. Hence my posts are not 100% accurate.

What c5 core version are you using?

Have a look at the actual column data and build some simple tests based on that.

You can try leaving off the ak_ prefix. You can try leaving out 'Like' (I think it defaults to equals), and various combinations. Get it working for some obvious data in a simple case, then add the complexities back in.

I have some code that did something like this a while back. I will see if I can dig it out tomorrow.
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
(Chronic insomniac) Here is what the core user search builds for such a filter in an ecommerce site I work with. c5.6.3.3.
filter('ak_shipping_address_city', '%London%','like');


Note its using filter, not filterByAttribute. I expect that is because its filtering on a search column that is part of an attribute, not on a complete attribute.
rosie607 replied on at Permalink Reply
rosie607
Hi JohntheFish
Thank you and your insomnia :-)
I tried it as you suggested without the 'ByAttribute' and it's working perfectly.
Thank you!

$ul->filter('ak_location_city', "London", "LIKE");
zuna replied on at Permalink Reply
I'm not sure but maybe the problem it's that address it's an array and you need to pass the address attribute data first in an array variable and then try to make a comparison in a loop.
This it's an example i used to pass data from user to a page, you can see the address structure. Sorry but i haven't time today to think about this.

$Useradress=$ui->getAttribute('indirizo_struttura');

$newPage->setAttribute('street_address',array(
'address1'=>$Useradress->getAddress1(),
'address2'=>$Useradress->getAddress2(),
'city'=>$Useradress->getCity(),
'region'=>$Useradress->getStateProvince(),
'country'=>$Useradress->getCountry(),
'postal_code'=>$Useradress->getPostalCode(), ));