TOPIC:

Populate Fields from Database Table 12 years 4 months ago #12780

  • erichhaubrich's Avatar Topic Author
  • erichhaubrich
  • Offline
  • Junior Breezer
  • Junior Breezer
  • Web Ninja and Technology Geek
  • Posts: 53
  • Karma: 4
  • Thanks: 3
I looked around for a way to populate fields in a form from the database if a user is logged in. I hacked together a few pieces of code and came up with this. IT WORKS! - Note: I am using it in Quick Mode.

In this case, the table that I'm pulling from has the user's email address in it - this grabs the email address of the current user and finds that email in the table I am querying, then populates the field values of the form.

I am using it to populate the mobile phone and home phone of the user in a contact form.

Before Form Piece
// Get the lead info fo the current user
$this->execPieceByName('ff_InitLib');
global $database, $record, $my;
$id = $my->email;
$database->setQuery("
SELECT *
FROM jos_[your_table_name]
WHERE email = '$id'"
);
$row = $database->loadObjectList();
$record = $row[0];

FORM VALUES:

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

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

Home Phone:
<?php global $record; return $record->hphone; ?>

Mobile Phone:
<?php global $record; return $record->mobile; ?>

If you just want to populate the name and email then you can skip the Before Form Piece and just use the values for Name and Email.
I love Breezing Forms! - It is the Killer App for Forms on Joomla!

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

Re:Populate Fields from Database Table 12 years 4 months ago #12813

  • nblit
  • Offline
  • Fresh Breezer
  • Fresh Breezer
  • Posts: 16
  • Thanks: 0
Excellent, works like a charm

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

Re:Populate Fields from Database Table 12 years 4 months ago #13003

  • bgarland
  • Offline
  • Fresh Breezer
  • Fresh Breezer
  • Posts: 4
  • Thanks: 0
Has anyone done this for JomSocial data? Specifically, the jos_community_fields_values table.

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

Re:Populate Fields from Database Table 12 years 4 months ago #13020

  • erichhaubrich's Avatar Topic Author
  • erichhaubrich
  • Offline
  • Junior Breezer
  • Junior Breezer
  • Web Ninja and Technology Geek
  • Posts: 53
  • Karma: 4
  • Thanks: 3
I remember seeing some stuff for Community Builder, you may want to take a look at that to see if it helps.
I love Breezing Forms! - It is the Killer App for Forms on Joomla!

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

Re:Populate Fields from Database Table 12 years 4 months ago #13042

  • bgarland
  • Offline
  • Fresh Breezer
  • Fresh Breezer
  • Posts: 4
  • Thanks: 0
This is what I came up with, but it's throwing an error. Anyone see any obvious errors? I can run the SQL query in PhpMyAdmin without getting an error but the Before Form piece doesn't like something.

$this->execPieceByName('ff_InitLib');
global $database, $record, $my;
$id = $my->email;
$database->setQuery("
SELECT a.userid as `UserID`,
u.name as `Name`,
u.email as `Email`,
u.registerDate as `Register`,
u.lastvisitDate as `LastVisit`,
u.activation as `Activated`,
a.status as `Status`,
a.points as `Points`,
a.friendcount as `FriendCount`,
MAX(IF(b.field_id=4,b.value, NULL)) AS 'Hometown',
MAX(IF(b.field_id=8,b.value, NULL)) AS 'LandPhone',
MAX(IF(b.field_id=9,b.value, NULL)) AS 'Address'
FROM jos_community_users a
LEFT JOIN jos_community_fields_values b ON a.userid = b.user_id
LEFT JOIN jos_community_fields f ON f.id = b.field_id
LEFT JOIN jos_users u ON u.id = a.userid
WHERE u.email = '$id'
GROUP BY a.userid
ORDER BY u.name, a.userid"
);
$row = $database->loadObjectList();
$record = $row[0];

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

Re:Populate Fields from Database Table 12 years 3 months ago #13176

  • erichhaubrich's Avatar Topic Author
  • erichhaubrich
  • Offline
  • Junior Breezer
  • Junior Breezer
  • Web Ninja and Technology Geek
  • Posts: 53
  • Karma: 4
  • Thanks: 3
Can you post the error that you're getting? - If possible post the line(s) of code that the error refers to when you view source on the page.

We'll get it figured out. :)
I love Breezing Forms! - It is the Killer App for Forms on Joomla!

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

Moderators: ForumSupport
Time to create page: 0.052 seconds

BreezingForms Pro 1.4.7 for WordPress Released!

Available in the membership section.

September Discount!

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!