Populate a field from a database table

  • isouthwell
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 15
  • Thank you received: 0

isouthwell created the topic: Populate a field from a database table

Hi there,

I'm trying to auto populate a field from a MySql database table.

I can populate a field from the Joomla table by adding the following to the 'value' in my form:

<?php $user = &JFactory::getUser();return $user->username;?>

It puts in the username. Great!

I have another table called jsn_users with a field name 'shandicap' which is a number I want to autopuluate on the form.

If I use the following is doesn't work:

<?php $user = &JFactory::getUser();return $user->shandicap;?>

I can't figure out how to get this working - how can I tell BF to look in the jsn_users table and use shandicap.

I've searched the forums and looked at the support documents, but I can't figure it out - would really appreciate some help on this.

Many thanks

Ian
#148612
  • Posts: 6982
  • Karma: 187
  • Thank you received: 628

chrisal replied the topic: Populate a field from a database table

Hello isouthwell,


unfortunately this works only with values of the Joomla! user object and not custom values from other database tables. To get custom values from the database - you can use the following code:
<?php $db = JFactory::getDbo(); $query = 'Select shandicap from #__jsn_users where XXXXXX'; $db->setQuery($query); $value = return $db->loadResult();?>
The last part of the query(XXXXXX) is the tricky one. There has to be some kind of connection between the default users table and the jsn users table e.g: name, user id.

Is it possible to send me a sample of what the jsn_users table values look like?


regards
Chris

Regards,
Christian

===============================================
Satisfied? Consider a membership!
===============================================
Like us on Facebook
===============================================
Follow us on Twitter
===============================================
#148668
  • isouthwell
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 15
  • Thank you received: 0

isouthwell replied the topic: Populate a field from a database table

Thanks Chris, really appreciate your help.

I've attached a screenshot of the table. I made 2 new fields called shandicap and chandicap in an extension called easy profile, so I would imagine there is a link to users....

Yours,

Ian
#148674
Attachments:
  • isouthwell
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 15
  • Thank you received: 0

isouthwell replied the topic: Populate a field from a database table

Hi Chris,

I'm quite keen to resolve this one issue that is holding up the form being published. did you miss the screenshot I sent? Many thanks indeed,

Ian
#148744
  • Posts: 6982
  • Karma: 187
  • Thank you received: 628

chrisal replied the topic: Populate a field from a database table

Hello again,

sorry for the delay.


Okay - please try it with this code:
<?php $id = JFactory::getUser()->get("id"); $db = JFactory::getDbo(); $query = 'Select shandicap from #__jsn_users where id='.$id.''; $db->setQuery($query); return $db->loadResult();?>


regards
Chris

Regards,
Christian

===============================================
Satisfied? Consider a membership!
===============================================
Like us on Facebook
===============================================
Follow us on Twitter
===============================================
#148820
The following user(s) said Thank You: isouthwell
  • isouthwell
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 15
  • Thank you received: 0

isouthwell replied the topic: Populate a field from a database table

Chris, thank you very much for your help on this. Your solution did work - however, I actually added a couple of fields (shandicap and chandicap) directly to the user database (so it was all together) and then I used the following code. I really didn't understand this but eventually figured it out.

$this->execPieceByName('ff_InitLib'); //Include BreezingForms Library
$db = JFactory::getDBO();//Get Database Object
$user = &JFactory::getUser();//Get user Object

//Create your own query
$db->setQuery('Select shandicap From #__users Where id='.$user->id);

$result = $db->loadResult(); //load the result from the query
ff_setValue('shandicap', $result); // populate the value in the form. Change shandicap to the real name of your element

Thanks again for your help :)
#149671
Moderators: ForumSupporttomeperica
Time to create page: 0.081 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