Welcome, Guest

Populate Fields from Database Table
(1 viewing) (1) Guest

TOPIC: Populate Fields from Database Table

Populate Fields from Database Table 1 year, 11 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 1 year, 10 months ago #12813

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

Re:Populate Fields from Database Table 1 year, 10 months 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 1 year, 10 months 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 1 year, 10 months 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 1 year, 10 months 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.41 seconds

About

Crosstec GmbH & Co. KG

Bergisch-Gladbacher-Str. 829

51069 Cologne, Germany