Forums

How to automatically fill up from two or multiple database tables and save new records to custom table

phicts 10 Jan, 2016
How to autofill my form fields based on three or multiple Joomla tables?

table1 is from Joomla core user table:
User ID
User Name
User Email

table 2 is also from Joomla core profile table
User Address
User City
User Phone

table 3 is a custom table that references the User ID
User ID (autofill based on table 1)
Transaction ID (autoincrement)
Transaction Uniq_id
Transaction Type
Transaction Detail

All fields from all tables (1,2 and 3) will be emailed. Only the fields in Table 3 will be saved to its table.

My problem is how to display these in just one form. Or do I need three forms? How do I connect the three forms? I've been browsing the forum but the explanations are too technical for me.😟 Please provide a related tutorial, something like "what to put where" please.
phicts 10 Jan, 2016
I have already seen and followed this guide, http://www.chronoengine.com/faqs/56-cfv4/cfv4-other-faqs/2586-how-can-i-get-in%3Cspan%20class=
But what about the user's address, city and phone from the Joomla profile table?
What is the code and where to put them? Please give me an example code. Thank you.
GreyHead 10 Jan, 2016
Hi phicts,

You already have the User ID so you can use a DB Read to get data for the user from the profile table. IIRC the table uses multiple records for each user so you will need to set the DB Read to multiple and may then need to clean up the results in a Custom Code action to get them into a usable form.

Bob
phicts 10 Jan, 2016
HI Bob. Thanks for replying. I didn't understand what you want me to do though. I just don't know how to do it.
What I've managed so far is to automatically show the username, email and user id of the logged-in user, by following the pertinent FAQ/tutorial. The code is:
<?php
$user =& JFactory::getUser();
$form->data['email'] = $user->email;
$form->data['name'] = $user->name;
$form->data['id'] = $user->id;
?>

<div class="ccms_form_element label_over" id="name_container_div" style="">
  <label for="name" size="250">Name</label>
  <input id="name" value="<?php echo $name; ?>" name="name" 
    type="text" <?php echo $readonly; ?> />
</div>

<div class="ccms_form_element label_over" id="name_container_div" style="">
  <label for="email" size="250">Email</label>
  <input id="email" value="<?php echo $email; ?>" name="email" 
    type="text" <?php echo $readonly; ?> />
</div>

<div class="ccms_form_element label_over" id="name_container_div" style="">
  <label for="id" size="11">User ID</label>
  <input id="id" value="<?php echo $btcaddress; ?>" name="id" 
    type="text" <?php echo $readonly; ?> />
</div>


I also found a tutorial on how to show some data from Joomla's user profile table. For example, to show the user's address when it has been answered/provided by the logged-in user:
<?php
jimport( 'joomla.user.helper' ); 
$user = JFactory::getUser();
$userId = $user->id; 
$userProfile = JUserHelper::getProfile( $userId );

echo "Main Address :" . $userProfile->profile['address1'];
?>


However, I do not know how to "convert" the above code to show like as a field that can be saved in the table like in the first code displaying the name, email and id of the logged in users.

Worse, I realized belatedly that the address and other data that I would like to show/auto-populate in their respective fields come from a custom table made by a Joomla extension called Easy Profile, http://extensions.joomla.org/extension/easy-profile. The table is called #_jsn_users and the fields I want to fetch are "address" and "cpnumber"

Please see attached image for reference.
GreyHead 10 Jan, 2016
Hi phicts,

Assuming that the name of the User Address input is user_address then you can use your code above like this
. . .
$form->data['user_address'] = $userProfile->profile['address1'];
. . .

I'm afraid that I don't know anything about how EasyProfile stores the data but a DB Read using the User ID will probably load it.

Bob
phicts 10 Jan, 2016
Here's the "API" of Easy Profile to manipulate the data stored in its table http://docs.easy-profile.com/index.php/article/jsnuser-object

The sample codes shown on their site are almost similar to the codes we use to fetch the user name, email and id of the logged in users. Almost similar but I really don't get it, being a PHP noob. 😟
GreyHead 10 Jan, 2016
Hi phicts,

As you say it looks very similar to the Joomla! User code. You should be able to read the entries you need in pretty much the same way.

Bob
phicts 11 Jan, 2016
Still no luck with the trial and error approach to auto-fill that address field.😟

Please help me with the DB Read code as you suggest. What to put where, please?

The table name from which I want to get the data is #_jsn_users and the field/column name I want to fetch/auto-fill is ceoaddress (and if I may abuse, would also like to show the cpnumber but it's not really necessary).

The two tables, joomla users and jsn users, have the same ID for the same users.

Thanks.
Max_admin 14 Jan, 2016
Hi,

The "DB Read" action has a "Relations" section, if you setup this correctly then you can read the user's record from the Joomla users table and any associated records from the related tables.

Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
This topic is locked and no more replies can be posted.