TOPIC:

Get Value from Custom USER Fields 1 year 11 months ago #229035

  • Topic Author
  • gvalcho
  • Offline
  • Junior Breezer
  • Junior Breezer
  • Posts: 25
  • Thanks: 0
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

Please Log in or Create an account to join the conversation.

Get Value from Custom USER Fields 1 year 11 months ago #229036

  • DarkoCujic's Avatar
  • DarkoCujic
  • Offline
  • Developer
  • Developer
  • Posts: 2154
  • Karma: 67
  • Thanks: 264
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!

Please Log in or Create an account to join the conversation.

Last edit: Post by DarkoCujic.

Get Value from Custom USER Fields 1 year 11 months ago #229037

  • Topic Author
  • gvalcho
  • Offline
  • Junior Breezer
  • Junior Breezer
  • Posts: 25
  • Thanks: 0
Thank you for posting the preferred method, I will try it.

GV

Please Log in or Create an account to join the conversation.

Get Value from Custom USER Fields 1 year 11 months ago #229038

  • DarkoCujic's Avatar
  • DarkoCujic
  • Offline
  • Developer
  • Developer
  • Posts: 2154
  • Karma: 67
  • Thanks: 264
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!

Please Log in or Create an account to join the conversation.

Get Value from Custom USER Fields 1 year 11 months ago #229148

  • Topic Author
  • gvalcho
  • Offline
  • Junior Breezer
  • Junior Breezer
  • Posts: 25
  • Thanks: 0
I finally tried the "one liner" above, and found that it does not return a value.

Thanks, George

Please Log in or Create an account to join the conversation.

Get Value from Custom USER Fields 1 year 11 months ago #229349

  • DarkoCujic's Avatar
  • DarkoCujic
  • Offline
  • Developer
  • Developer
  • Posts: 2154
  • Karma: 67
  • Thanks: 264
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!

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
  • 2
Moderators: ForumSupport
Time to create page: 0.052 seconds

Summer Sale!

Massive discounts on all subscriptions!

Get Your Subscription Here

Quick Links

Downloads

BreezingForms

ContentBuilder

BreezingCommerce

Templates

Documentation

BreezingForms

ContentBuilder

BreezingCommerce

Apprendre BreezingForms (French Community)

Apprendre et maîtriser BreezingForms par des tutoriels et exemples, le tout en français

breezingforms.eddy-vh.com

Questions et réponses sur les forums de l'AFUJ

AFUJ

Special Offer

Summer Sale! All subscriptions at a special price!

Includes prio support, all of our current and future Joomla!® extensions and Joomla!® templates for the duration of your membership.

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!