Get Value from Custom USER Fields

  • gvalcho
  • Topic Author
  • Offline
  • Junior Boarder
  • Junior Boarder
  • Posts: 25
  • Thank you received: 0

gvalcho created the topic: Get Value from Custom USER Fields

In Joomla you can add custom fields for user registration. These fields can be added in the user management (fields) area. I added Company Name for example. I struggled to figure out how to get this field out of the database and use the value for display. In this forum I found a similar post about getting data for a specific user, and from that I was able to make it work. I'm sharing the code here so that others can easily find it.

I put this code (below) into a BF element's value field to pull in the data.

<?php
$user = JFactory::getUser();
if ($user) {
$user_id = $user->id;
$field_id = "1";
$company = "";
$db = JFactory::getDbo();
$db->setQuery("SELECT value from xxxxx_fields_values WHERE field_id = $field_id AND item_id = $user_id");
$company = $db->loadResult();
return $company;
}
return "";
?>

Custom USER fields are stored in table "fields value", the xxxxx above is the table name prefix. Each field, when created, has a field ID. I found the field ID using phpMyAdmin.

I hope this helps someone in the future...

GV
#229035
  • Posts: 2154
  • Karma: 67
  • Thank you received: 264

DarkoCujic replied the topic: Get Value from Custom USER Fields

Hey,

thank you for the contribution.

If I may add a few things. Although your solution is correct and will work, Joomla recommends doing it this way:
// first line might not be necessary depending on Joomla version
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
$customFields = FieldsHelper::getFields('com_users.user', JFactory::getUser(), true);

If you were to echo out the contents of $customFields, you'd see how it was structured and how to get to a specific field. So, if you're willing to adapt your code to what I just wrote, simply do the two lines from above and add
echo '<pre>';
print_r($customFields);
echo '</pre>';

and follow the object structure to get to the field you need.

For example, I have a gender field for my users and it's the second field I created so to access and store it in an element called 'gender', I simply do the following:
$customFields = FieldsHelper::getFields('com_users.user', JFactory::getUser(), true);
ff_setValue('gender', $customFields[1]->value);

Just my two cents. :)

edit: If you want a one-liner to use in the Value property of an element, you can do:
<?php $customFields = FieldsHelper::getFields('com_users.user', JFactory::getUser(), true); return $customFields[1]->value; ?>

Kind regards,
Darko

Satisfied with our support? PayPal tips
===========
Need to renew Pro Support? Buy Here!
===========
Satisfied? Consider a membership!
===========
Like us on Facebook
===========
Support Crosstec and get ALL EXTENSIONS and professional support for just $5. Here!
#229036
  • gvalcho
  • Topic Author
  • Offline
  • Junior Boarder
  • Junior Boarder
  • Posts: 25
  • Thank you received: 0

gvalcho replied the topic: Get Value from Custom USER Fields

Thank you for posting the preferred method, I will try it.

GV
#229037
  • Posts: 2154
  • Karma: 67
  • Thank you received: 264

DarkoCujic replied the topic: Get Value from Custom USER Fields

No problem :)

Regards,
Darko

Satisfied with our support? PayPal tips
===========
Need to renew Pro Support? Buy Here!
===========
Satisfied? Consider a membership!
===========
Like us on Facebook
===========
Support Crosstec and get ALL EXTENSIONS and professional support for just $5. Here!
#229038
  • gvalcho
  • Topic Author
  • Offline
  • Junior Boarder
  • Junior Boarder
  • Posts: 25
  • Thank you received: 0

gvalcho replied the topic: Get Value from Custom USER Fields

I finally tried the "one liner" above, and found that it does not return a value.

Thanks, George
#229148
  • Posts: 2154
  • Karma: 67
  • Thank you received: 264

DarkoCujic replied the topic: Get Value from Custom USER Fields

Hey,

I'm pretty sure there is an error in copy/pasting as it works without a problem on my test environments.

But, whatever works :)

Kind regards,
Darko

Satisfied with our support? PayPal tips
===========
Need to renew Pro Support? Buy Here!
===========
Satisfied? Consider a membership!
===========
Like us on Facebook
===========
Support Crosstec and get ALL EXTENSIONS and professional support for just $5. Here!
#229349
Moderators: ForumSupporttomeperica
Time to create page: 0.075 seconds

New Icon Packs Category!

Crosstec is now offering icon packs.

If you are a paying subscriber, icon packs are automatically added to your account.

Check out our icon packs page!

Live Support Chat Opened!

Join our Discord chat here to receive live support and talk directly to the team!

Summer Sale!

50% discount on all of our extension subscription plans, templates and icon packs!

Get Your Subscription Here

News and Updates

Get informed about new downloads, updates and more in our News and Updates newsletter.

All Extensions Subscription

Get 1 year access to all of our current and future products and 1 year of professional support -- 99 for just 49! (Summer Sale)

No support per domain or website installation limits! Includes all of our current and future Joomla!® extensions, Joomla!® templates for the duration of your membership. This means, by purchasing an All Extensions Subscription you'll have it all covered!

Get it from here

3rd Party Discount - 25% Off

We help you to keep your costs under control. If you are a new member and purchased a form building tool from a different form vendor, then you'll get a 25% discount on our subscription plans.

How to receive the discount:

Send us a quick email to sales@crosstec.org with a proof of purchase (for example a paypal receipt), await payment instructions and enjoy your membership!

Live Support Chat Opened!

Join our Discord chat here to receive live support and talk directly to the team!

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here