Forums

Problem loading Data from two tables

jinx52 10 Jun, 2012
Hi,
I'm having an issue trying to load data from two separate tables in one form. Basically what I'm hoping to achieve is to read info from the user registration table and then go and get more info about each user from another table I have set up, and then display it all.
I have set up a db multiloader with the following in the where clause;
<?php
$where = array();
$where[] = "`id` > 0";
$where = implode(' AND ', $where);
echo $where;
?>
 ORDER BY UserDetails.name

I've done it like this because I want to add more conditions latter. I'm loading data into the Model ID UserDetails.
Next I have placed a DB Loader into the "On Record Found' event of the DB multi loader (I assume this is the right place for it?) I have the following code in the where clause of the DB loader.
<?php
$detail = $form->data['UserDetails']; ?>
`cf_user_id` = '<?php echo $detail['id']; ?>'

I am loading this info into the UserPersonal Model ID.
Finally I have a custom code element to create the table with all the data.
<table frame="box" width="700" cellpadding="5">
<?php foreach($form->data['UserDetails'] as $detail): 
$userpersonal = $form->data['UserPersonal'];?>
<tr>
<td><?php echo $detail['id']; ?></td>
<td><?php echo $detail['name']; ?></td>
<td><?php echo $userpersonal['input_phone']; ?></td>
<td><?php echo $detail['email']; ?></td>
</tr>
<?php
endforeach;
?>
</table>

When the form runs, I get the info from the multidata loader, but not from the other table. Adding a debugger gives me the following;

Clearly the 'Userdetail' data is not been seen in the DB loader, as there is nothing in the 'where' clause, but that data is been seen subsequently in the custom code to display in the table!
I'm confused!
Any help would be appreciated.
Nick
jinx52 15 Jun, 2012
Hi Again,

I have resorted to writing a table read routine directly in PHP (with associated DB connection, etc) to get around this issue. Not great, as it it effectively makes the chronosform solution obsolete and leaves me with code I have to maintain.

Can I ask, am I simply using the wrong product here? Is reading from two tables something I should be using Chrono Connectivity for instead?

Thanks,

Nick
GreyHead 19 Jun, 2012
Hi Nick,

You can read from two tables in either ChronoForms or ChronoConnectivity.

In this case though you can get all of the User details from the jos_users table by calling the Joomla! User object. Either use the code
<?php
$user =& JFactory::getUser();
?>
or, if you want it all get my Load User Info [GH] action which will do that for you and put all the user info into the $form->data array.

Once you have the User ID in the $form->data array you should have no problem with the DR Record Loader query.

Bob
jinx52 23 Jun, 2012
Thanks Bob,
I will check that out.
Regards,
Nick
GreyHead 23 Jun, 2012
Hi Nick,

I gathered a bit more information into this FAQ

Bob
jinx52 25 Jun, 2012
Thanks again Bob, although I'm not really sure this solves my problem as it appears to load the current user into the $form->data array. My problem is different. I want to
1. Load all the users into the $form->data array. I currently do this via a DB multiloader event to go through and read all the records.
2. Print them out in a table. Again no problem, I do this by customer code after the multiloader that goes through and prints the table.
My problem is that I also want to include data from another table. So for each item in the $form->array I have to go to another table and read some more info, based on a key value. At present I'm doing this the hard way - hard coding the read for each record into the custom code, because I don't know how to call a single db read event from custom code.
I would have thought I could do it as part of the original multiload. Read each record into the $form->data array then go off an read another table based on a key value, but I can't get that to work either.
I hope that makes my issue a little clearer.
Thanks,
Nick
GreyHead 25 Jun, 2012
Hi Nick,

You can link two (or more) DB Multi-Record loader actions into a single query using the Advanced tab settings to set up a JOIN between the two tables.

This is a little tricky to get the correct settings - there is a thread about it somewhere here that I used. I'll try to find it and put up a new FAQ later.

Bob

PS Max has written this FAQ for doing something similar with ChronoConnectivity.
This topic is locked and no more replies can be posted.