|
Re: [SMARTY] How do I group db results by category with Smarty?: msg#00263php.smarty.general
Thanks Kelly This is what I originally tried, but got confused, so I turned to Tim's solution, which worked, as you can se from my other post. It is simple too when using object arrays as I did - I had to use only one foreach block for everyhting. I definately will try your advice too, it seems interesting. I wonder if there are some performace differences between these too solutions? Not in my case, but when data amount increases, there might be. gawan ----- Original Message ----- From: "Kelly Cochran" <kcochran@xxxxxxxxxxx> To: "gawan" <mail@xxxxxxxxx> Cc: <smarty-general@xxxxxxxxxxxxx> Sent: Thursday, January 16, 2003 10:59 PM Subject: Re: [SMARTY] How do I group db results by category with Smarty? > > When I do something like this, I tend to do the data setup within the > PHP itself. Keeps the template simple. > > $res = $DB->query('SELECT * FROM tbl, cat_tbl WHERE tbl.cat_id = > cat_tbl.cat_id ORDER BY cat_id'); > while($r = $res->fetchRow()) > { > $data[$r['cat_name']][] = $r; > } > $tpl->assign('data', $data); > > and in the template: > {foreach key=cat item=d from=$data} > Category: {$cat} > {foreach item=entry from=$d} > {$entry.col_name} > {/foreach} > {/foreach} > > gawan wrote: > > > I finally got time get back to this problem. > > > > I tried something like this (cat_id is here genre_id and > > cat_description is > > genre_description): > > > > {$foo eq ''} > > {foreach from=$db_result item=current key=PISBN} > > > > {if $foo ne > > $current->genre_id}{$current->genre_description}{/if} > > > > > > > > > > {$foo eq $current->genre_id} > > {/foreach} > > > > I kind of see why this will not work, but I can't figure out how this > > should > > be written...!? > > > > thanks for any further advice > > > > gawan > > > > ----- Original Message ----- > > From: "Tim Burden" > > To: "gawan" ; > > Sent: Tuesday, January 07, 2003 2:25 AM > > Subject: Re: [SMARTY] How do I group db results by category with Smarty? > > > > > > > > >SELECT * FROM table1,table2 WHERE table1.cat_id = table2.cat_id AND text > > >LIKE $q GROUP BY table1.cat_id. > > > > > >Then in the template, set $foo = '', loop through this result set, > > setting > > >$foo = $cat_id at the end of each iteration, then print > > cat_description if > > >$foo != $cat_id > > > > > >would be one way. > > > > > >----- Original Message ----- > > >From: "gawan" > > >To: > > >Sent: Monday, January 06, 2003 6:33 PM > > >Subject: [SMARTY] How do I group db results by category with Smarty? > > > > > > > > > > > >>I have a table with data and an other table with category information > > > > for > > > > >>the data > > >> > > >>table1: id, title, text, cat_id > > >>table2: cat_id, cat_description > > >> > > >>It is easy to display data from table 1 with smarty. I fetch the data as > > > > > >an > > > > > >>object array an display these with a smarty foreach loop. > > >>But I don't know how to display the data organized by category and use > > > > the > > > > >>gategory name (cat_description as a caption for each group). > > >>(The number of categories in each case after a search is unknown, as are > > > > > >the > > > > > >>number of rows in each gategory, if any) > > >>Does anyone know an example of using Smarty to display hierarcical data > > > > > >this > > > > > >>way. I was hoping to render the data like this: > > >> > > >>Caption for category one (from cat_description) > > >> A row of data > > >> A row of data > > >> A row of data > > >>Caption for category two > > >> A row of data > > >> A row of data > > >> A row of data > > >>Caption for category three > > >> A row of data > > >> A row of data > > >> A row of data > > >>: > > >>etc > > >> > > >>Any advise on how to start tackling this challence? > > >>thank you very much > > >> > > >>gawan > > -- - > Kelly Cochran > http://www.trolans.net/ > -- Smarty General Mailing List (http://smarty.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: [SMARTY] How do I group db results by category with Smarty? - a solution: 00263, gawan |
|---|---|
| Next by Date: | [SMARTY] Form check box: 00263, Juzan Jean-Michel |
| Previous by Thread: | Re: [SMARTY] How do I group db results by category with Smarty?i: 00263, Kelly Cochran |
| Next by Thread: | Re: [SMARTY] How do I group db results by category with Smarty?: 00263, gawan |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |