Re: [SMARTY] multiple nested queries?

"messju mohr" <[email protected]>
Newsgroups gmane.comp.php.smarty.general
Message-ID <20050808193828.GA9554@dune>
On Mon, Aug 08, 2005 at 02:24:34PM -0500, 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?

assign a two-dimensional array, loop through it with two nested
foreach- or section-loops.

for example make your sql produce a structure like this:

array('main1' => 'label' => 'Label1',
                 'subitems' => array('sub11' => 'Label1.1',
                                     'sub12' => 'Label1.2',
                                     ...
                                    ),

      'main2' => 'label' => 'Label2',
                 'subitems' => array('sub21' => 'Label2.1',
                                     'sub22' => 'Label2.2',
                                     ...
                                    ),
      ...
);

(where "main*" and "sub*" are the ids, and "Label*" are the names)

then display it in the template with:

{foreach from=$menu key=id item=main}
   {$id}: {$main.label}

   {* display submenu of this element: *}
   {foreach from=$main.subitems key=sub_id item=sub_label}
     {$sub_id}: {sub_label}
   {/foreach}
{/foreach}


of course there are a million other ways to structure this.

HTH
messju

> Thanks,
> Amanda
> 
> -- 
> Smarty General Mailing List (http://smarty.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php

-- 
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.