|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
GeXus Smarty Rookie
Joined: 16 Aug 2006 Posts: 28
|
Posted: Sun Jan 14, 2007 2:25 am Post subject: Two associated queries |
|
|
I trying to loop through categories and their associated sub categories by basically doing the following example
Code: |
$query = "select * from categories"
while ($row = $query)
{
echo "cat: $row['cat_name'];
$query = "select * from sub_categories where cat_id = $row['id']"
while ($row = $query){
echo "sub cat: $row['sub_name'];
{
} |
how would I do this using smarty? So that I can format the sub-categories and categories seperatly and have them display appropriately?
Thank You! |
|
Back to top |
|
Pap Smarty Regular
Joined: 21 Jun 2006 Posts: 69 Location: Denver, CO
|
Posted: Sun Jan 14, 2007 3:23 am Post subject: |
|
|
All code untested, so please mind any typos.
Code: | $sql = 'SELECT * FROM categories INNER JOIN sub_categories ON (categories.id = sub_categories.cat_id)';
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$categories[$row['cat_name']][] = $row['sub_name'];
}
$smarty->assign('categories', $categories); |
Code: | {foreach from=$categories key='cat_name' item='category'}
{$cat_name}:
{foreach from=$category item='sub_category'}
{sub_category},
{/foreach}
{/foreach} |
Reading the documentation on {foreach} will benefit you. You may also need to sort the results of that query.
You're probably new at this so I suggest using PEAR::DB, if only for the way it simplifies performing queries and extracting query data. _________________ Don't be stupid, be a Smarty™.
Come and join the P-H-Party. |
|
Back to top |
|
GeXus Smarty Rookie
Joined: 16 Aug 2006 Posts: 28
|
Posted: Sun Jan 14, 2007 9:58 pm Post subject: |
|
|
Thanks! That worked.. however when I also wanted to get the category_id and sub_category_id, i didn't seem to work and i can't exactly figure out how to change it so it will... any help would be great! |
|
Back to top |
|
Pap Smarty Regular
Joined: 21 Jun 2006 Posts: 69 Location: Denver, CO
|
Posted: Sun Jan 14, 2007 11:01 pm Post subject: |
|
|
I bet the problem you were having was with the query itself...
Code: | $sql = 'SELECT categories.id AS cat_id, sub_categories.id AS sub_id, category_name, sub_name FROM categories INNER JOIN sub_categories ON (categories.id = sub_categories.cat_id)';
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$categories[$row['cat_name']][] = array('sub_id' => $row['sub_id'], 'cat_id' => $row['cat_id'], 'sub_name' => $row['sub_name']);
}
$smarty->assign('categories', $categories); |
Code: | {foreach from=$categories key='cat_name' item='category'}
{$cat_name}:
{foreach from=$category item='sub_category'}
{sub_category.sub_id}, {sub_category.cat_id}, {sub_category.sub_name}
{/foreach}
{/foreach} |
Still untested. As I reconsider the original problem, this may not have been the best way to do it. All I'm trying to accomplish here is to get you familiar with Smarty syntax. _________________ Don't be stupid, be a Smarty™.
Come and join the P-H-Party. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|