Nested Queries
| From: | Gary Smith | Date: | Wed, 10 Aug 2005 00:55:43 +0000 |
| Subject: | Nested Queries | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-12936@lists.php.net to get a copy of this message | ||
Aloha All,
I have a database that has two tables CUSTOMERS and RESERVATIONS. A
customer can have multiple reservations. I would like to display the
customers name and the types of reservations they made. Without templates I
would embed a second query within the results of the first query to find all
reservations with the current customerID. I can¹t figure out how to do this
with templates.
I have a file setup as such:
index.html
$query=
<<<SQL
SELECT *
FROM CUSTOMERS
INNER JOIN RESERVATIONS
ON CUSTOMERS.customerID = RESERVATIONS.customerID
ORDER BY CUSTOMERS.customerLastName ASC, CUSTOMERS.customerFirstName ASC
SQL;
$data = $DB->GetAssoc($query);
$tpl->assign('data', $data);
In my template I have :
index.tpl
{foreach from=$data item="entry"}
<tr align="left" valign="middle" bgcolor="{cycle
values='#dedede,#eeeeee'}">
<td>{$entry.customerFirstName|cat:"
"|cat:$entry.customerLastName}</td>
<td>{$entry.reservationType}</td>
</tr>
{foreachelse}
<tr align="left" valign="middle">
<td colspan="2">No records</td>
</tr>
{/foreach}
Thanks,
Gary