By Aikaterini Koutsouflaki on Friday, 04 January 2019
Posted in Technical Issues
Replies 11
Likes 0
Views 578
Votes 0
Hello
I am able to retrieve and echo the viewed profile's address where i need it, but i do not want to echo the whole address. I need to echo only the country! How can i do that?

I want to echo this info in another component enviroment.

Can you please advise?

Thank you in advance
Hey Aikaterini,

I am really sorry for the delay of this reply as it is a weekend for us here. May I know which particular custom field type are you using? If you are using the "Address" custom field, you can try something like this,

[gist]
// Assuming that the $id is the user's id
$user = ES::user($id);

// Assuming that the unique key for the address field is ADDRESS
$address = $user->getFieldValue('ADDRESS');

echo "<pre>";
var_dump($address);
echo "</pre>";
[/gist]
·
Friday, 04 January 2019 23:35
·
0 Likes
·
0 Votes
·
0 Comments
·
Thank you for your reply.
I am not using anything for the moment.

I tried...
require_once(JPATH_ADMINISTRATOR . '/components/com_easysocial/includes/easysocial.php');
$user = ES::user($id);
echo $user->getFieldValue( 'ADDRESS' );

and i got the whole address.

BUT i want just the country! So i would like to get something like...
echo $user->getFieldValue( 'COUNTRY' );

Is it possible? And how?

Thank you
·
Saturday, 05 January 2019 04:30
·
0 Likes
·
0 Votes
·
0 Comments
·
Hey there,

I am really sorry for the delay of this reply as it is a weekend for us here.

Firstly, you need to understand what this function does getFieldValue, the first parameter it actually respect the custom field unique key.

Can you double check with your current workflow and see what is that unique key it show from that custom field? http://take.ms/pAKCa

[gist type="php"]
$address = $user->getFieldValue('put your field unique key here');
[/gist]

If your current code is something like this, so you can just dump it and see what is the $address variable value it return.

For example :
[gist type="php"]
var_dump($address);exit;
[/gist]

Then refresh the page and see what is the value it return.
·
Saturday, 05 January 2019 10:53
·
0 Likes
·
0 Votes
·
0 Comments
·
Maybe i was not clear enough on my question. So here is the whole situation...

During registration the user completes the ADDRESS custom field that contains "address", "city", "state", "country". (country and the states are populated from database)

No COUNTRY field exists. The only country field that the user provides is inside the ADDRESS field!

Now...
when i view the user's profile, i need to get only the country.

On the previous post of yours that i tested...

require_once(JPATH_ADMINISTRATOR . '/components/com_easysocial/includes/easysocial.php');
$user = ES::user($id);
$address = $user->getFieldValue('ADDRESS');
var_dump($address);

The above returns...
object(SocialFieldsUserAddressValue)#2070 (8) { ["unique_key"]=> string(7) "ADDRESS" ["element"]=> string(7) "address" ["field_id"]=> string(3) "105" ["uid"]=> int(0) ["type"]=> string(4) "user" ["value"]=> object(SocialFieldsUserAddressObject)#2031 (9) { ["address1"]=> string(0) "" ["address2"]=> string(0) "" ["city"]=> string(0) "" ["state"]=> string(0) "" ["zip"]=> string(0) "" ["country"]=> string(0) "" ["latitude"]=> string(0) "" ["longitude"]=> string(0) "" ["address"]=> string(0) "" } ["raw"]=> string(0) "" ["data"]=> string(0) "" }

Hope i made it more clear now.
·
Saturday, 05 January 2019 21:43
·
0 Likes
·
0 Votes
·
0 Comments
·
I assume the user custom field haven't store in database yet before it execute that code.

May i know where did you put this code?

And which event trigger you use?
·
Monday, 07 January 2019 13:46
·
0 Likes
·
0 Votes
·
0 Comments
·
The user custom field ADDRESS is stored in database and it shows fine when i echo it.

For example

$address = $user->getFieldValue('ADDRESS');

gives me the whole address that is "address of the user" - "state of the user" - "country of the user". This is fine!

These data are populated from #__social_fields_data where the "datakey" is address.

I need to get from #__social_fields_data where the "datakey" = country! The field is there and i can see it!

Please check screenshot. It clearly shows what i need.
·
Monday, 07 January 2019 14:55
·
0 Likes
·
0 Votes
·
0 Comments
·
Hi Aikaterini,

Maybe you can try the code below and see how it goes?
[gist type="php"]
// Assuming that the $id is the user's id
$user = ES::user($id);

// Assuming that the unique key for the address field is ADDRESS
$address = $user->getFieldValue('ADDRESS');

echo "<pre>";
var_dump($address->value->country);
echo "</pre>";
[/gist]

You're suggested to backup the file that you want to customize beforehand for precaution
·
Monday, 07 January 2019 18:55
·
0 Likes
·
0 Votes
·
0 Comments
·
Unfortunately it did not work.

The result was string(0) ""
·
Monday, 07 January 2019 19:32
·
0 Likes
·
0 Votes
·
0 Comments
·
Solved like this...

require_once(JPATH_ADMINISTRATOR . '/components/com_easysocial/includes/easysocial.php');
$user_social = ES::user($id);
$address = $user_social->getFieldValue('ADDRESS');
$country = $address->value->country;
echo $country;

Thank you for your kind help!
·
Monday, 07 January 2019 23:22
·
0 Likes
·
0 Votes
·
0 Comments
·
Great! Glad to hear your issue has resolved Aikaterini

Just for your information, I have locked this thread as resolved to avoid confusions in the future. Please start a new thread if you have any other issue in the future so it will be easier for us to manage your inquiries.

Thanks for understanding and have a nice day ahead
·
Tuesday, 08 January 2019 10:26
·
0 Likes
·
0 Votes
·
0 Comments
·
View Full Post