|
Re: [SMARTY] How do I group db results by category with Smarty? - a solutio: msg#00262php.smarty.general
Hi I solved my problem and in case of someone else might be interested I'll attach here a summary of how this can be done: ---- my original question ---- 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 ---- answer from Tim Burden ---- 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. ---- my succesfull solution following Tims suggestion ---- After a joined query as in Tim's example I made the following template: (I used mysql_fetch_object in my query) {assign var="foo" value=""} {foreach from=$db_result item=current key=id} {if $foo != $current->cat_id} <tr> <td colspan="10">{$current->cat_description}<b/></td> </tr> {/if} <tr> <td>{$current->field2}</td> <td>{$current->field3}</td> <td>{$current->field3}</td> <!-- more field values --> </tr> {assign var="foo" value=$current->cat_id} {/foreach} So the modification to Tim's solution was to use Smarty's assign function to declare and assing values to a variable only used in a template. thanks for the help Tim gawan -- 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?: 00262, gawan |
|---|---|
| Next by Date: | Re: [SMARTY] How do I group db results by category with Smarty?: 00262, gawan |
| Previous by Thread: | Re: [SMARTY] How do I group db results by category with Smarty?i: 00262, gawan |
| Next by Thread: | [SMARTY] file permitions: 00262, Pupeno |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |