Welcome, Guest

TOPIC: Populate Fields from Database Table

Populate Fields from Database Table 2 years, 2 months ago #12780

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.
  • erichhaubrich
  • OFFLINE
  • Senior Boarder
  • Web Ninja and Technology Geek
  • Posts: 62
  • Karma: 3
I love Breezing Forms! - It is the Killer App for Forms on Joomla!

Re:Populate Fields from Database Table 2 years, 2 months ago #12813

Excellent, works like a charm
  • nblit
  • OFFLINE
  • Fresh Boarder
  • Posts: 17
  • Karma: 0

Re:Populate Fields from Database Table 2 years, 1 month ago #13003

Has anyone done this for JomSocial data? Specifically, the jos_community_fields_values table.
  • bgarland
  • OFFLINE
  • Fresh Boarder
  • Posts: 4
  • Karma: 0

Re:Populate Fields from Database Table 2 years, 1 month ago #13020

I remember seeing some stuff for Community Builder, you may want to take a look at that to see if it helps.
  • erichhaubrich
  • OFFLINE
  • Senior Boarder
  • Web Ninja and Technology Geek
  • Posts: 62
  • Karma: 3
I love Breezing Forms! - It is the Killer App for Forms on Joomla!

Re:Populate Fields from Database Table 2 years, 1 month ago #13042

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];
  • bgarland
  • OFFLINE
  • Fresh Boarder
  • Posts: 4
  • Karma: 0

Re:Populate Fields from Database Table 2 years, 1 month ago #13176

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.
  • erichhaubrich
  • OFFLINE
  • Senior Boarder
  • Web Ninja and Technology Geek
  • Posts: 62
  • Karma: 3
I love Breezing Forms! - It is the Killer App for Forms on Joomla!
Moderators: TheMuffinMan, ForumSupport
Time to create page: 0.40 seconds

Facebook Discount - 40% Off!

Fans of our Facebook page here are entitled to get a 1-year-subscription for the price of a 6-months!

Steps to take:

  • Become a fan on Facebook (Like) here
  • Purchase a 6-months-subscription from here
  • Write a quick email to This email address is being protected from spambots. You need JavaScript enabled to view it. with your username and prove of your like and we'll upgrade to the 1-year-subscription

Joomla!® Forms Discount - 40% Off!

Already purchased a forms extension from a different vendor but your requirements changed and now you need a forms extension that is capable of more than just the standards? We help you to keep the costs for a change under control: Get a 40% discount on our professional membership plan! -- meaning timely unlimited access to all current and future commercial extensions, breezingforms themes & apps and Joomla!® templates at crosstec.de, including 1 year of support -- without any website limitations -- Just prove that you previously purchased another forms extension for Joomla!® from a different vendor and you are qualified.

Steps to take:

  • Purchase a 1-year-subscription from here
  • Write a quick email to This email address is being protected from spambots. You need JavaScript enabled to view it. with your username and prove of the purchase for the other form vendor and we'll upgrade to the professional-subscription

About

Crosstec GmbH & Co. KG

Bergisch-Gladbacher-Str. 829

51069 Cologne, Germany

Imprint