By Andy on Monday, 03 June 2019
Posted in Technical Issues
Likes 0
Views 757
Votes 0
Hello. Can you advise what the PHP code would be to get the value of the "GENDER" and "BIRTHDAY" profile fields of the logged in user?

Thanks!
You can try this following suggestion code :

[gist type="php"]
// Find a way to retrieve the user id
$userId = '123';

// Current user data
$userData = ES::user($userId);

// Pass in gender unique key, you can found this from your workflow
$fieldData= $userData->getFieldValue('GENDER');

echo $fieldData;exit;
[/gist]


[gist type="php"]
// Find a way to retrieve the user id
$userId = '123';

// Current user data
$userData = ES::user($userId);

// Pass in birthday unique key, you can found this from your workflow
$fieldData= $userData->getFieldValue('BIRTHDAY');

echo $fieldData;exit;
[/gist]
·
Monday, 03 June 2019 09:57
·
0 Likes
·
0 Votes
·
0 Comments
·
Thanks Arlex.....

That's great. Works perfect for the Gender... probably for the Birthday too... but where my user has a 30th Nov 1972 birthday, and I've checked that in the profile, the code returns a 29th Nov 1972.

Any ideas on why that would be. Something to do with time zones I guess.. but it's odd if I edit the user profile it shows as 30th Nov.. just this code returning it as 29th.
·
Monday, 03 June 2019 10:12
·
0 Likes
·
0 Votes
·
0 Comments
·
It seems like I can't replicate this, can you show me how to replicate this on your site?

And which PHP file you apply those code?
·
Monday, 03 June 2019 10:24
·
0 Likes
·
0 Votes
·
0 Comments
·
I put the PHP into a module called "EasySocial GENDER Test" (module ID 560)

The code in that module is


<?php
$db =& JFactory::getDBO();
$user = JFactory::getUser();
$loggedin = $user->get('id');
if ($loggedin == 0) {
echo 'Log in to test this';
} else {
// Current user data
$userData = ES::user($loggedin);
// Pass in gender unique key, you can found this from your workflow
$fieldData= $userData->getFieldValue('GENDER');
echo $fieldData;
$fieldData= $userData->getFieldValue('BIRTHDAY');
echo $fieldData;
}
?>


The module shows at the top of this particular page: mysite /results-breeze

If you login with the SuperAdmin account, which has the DOB set to 30th Nov 1972... you'll then see that the module echo statement for DOB gives 29th Nov 1972.

Odd! Any ideas? Thanks for your time.
·
Monday, 03 June 2019 10:43
·
0 Likes
·
0 Votes
·
0 Comments
·
It seems like you are right, it did respected your timezone what you set from the brithday field.

And it seems like currently we do not have an option to skip this timezone value, for now I did help you updated to use this following custom code, it should display just fine now.

[gist type="php"]
$birthday= $userData->getFieldData('BIRTHDAY');

if (is_array($birthday) && isset($birthday['timezone'])) {
unset($birthday['timezone']);
}

$birthdayDate = new SocialFieldsUserDateTimeObject($birthday);
$birthdayValue = '';

if (!empty($birthdayDate->year) && !empty($birthdayDate->month) && !empty($birthdayDate->day)) {
$birthdayValue = $birthdayDate->year . '-' . $birthdayDate->month . '-' . $birthdayDate->day;
$birthdayValue = ES::date($birthdayValue, false)->format('d M Y');
}

echo $birthdayValue;
[/gist]
·
Monday, 03 June 2019 13:22
·
0 Likes
·
0 Votes
·
0 Comments
·
Fantastic. Thanks for looking into that and your expert guidance.
·
Monday, 03 June 2019 21:07
·
0 Likes
·
0 Votes
·
0 Comments
·
You are most welcome, glad that your issue is resolved now.
·
Monday, 03 June 2019 22:06
·
0 Likes
·
0 Votes
·
0 Comments
·
View Full Post