Re: [SMARTY] multiple nested queries?
| From: | Cameron Perry | Date: | Mon, 08 Aug 2005 19:48:20 +0000 |
| Subject: | Re: [SMARTY] multiple nested queries? | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-12929@lists.php.net to get a copy of this message | ||
The key is to write it into the same array, and keep track of which element is a top-level or sub-level item. You could achieve this with a while loop pretty easily, as long as you know the maximum depth of your tree. You could structure the array like this:
$item[$idx]['name']
$item[$idx]['id']
$item[$idx]['level'] //top- or sub-level
$item[$idx]['link']
... and your (pseudo)code:
$sql="SELECT * FROM main";
$result=mysql_query($sql);
while($mainRow=mysql_fetch_assoc($result))
{
//write $mainRow elements to $item array//$idx++;
$sql_sub="SELECT * FROM tabletwo where id=$mainRow[id]";
$result_sub=mysql_query($sql_sub);
if(mysql_num_rows($result_sub)>0)
{
while($subRow=mysql_fetch_assoc($result_sub))
{
//write $subRow elements to $item array
//$idx++;
}
}
}
...you get the idea
Once you have this array built you can stick it all into smarty as you normally do, but using your $item results instead.
HTH,
Cameron
On Aug 8, 2005, at 12:24 PM, Amanda Hemmerich wrote:
Hello! I'm pretty new to PHP and Smarty. I am trying to build a dynamic navigation bar using mysql, PHP, and smarty. I need to get the name and id of each menu item and then for each item, query a different table and get the sub-items associated with them. My normal way of using queries is to query the database and loop through the result with a smarty section and display the results. Is there a way I can loop through the results and build another query and then loop through THOSE results, too? Thanks, Amanda --Smarty General Mailing List (http://smarty.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php