Re: [SMARTY] Re: this mysql query with smarty

From: Date: Wed, 17 Mar 2004 21:54:11 +0000
Subject: Re: [SMARTY] Re: this mysql query with smarty
References: 1 2 3  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-11144@lists.php.net to get a copy of this message
Hello, what about left joining the table with itself? (I had to use some dummy aliases to get it working) SELECT dummy1.id, dummy1.name, dummy2.id, dummy2.name FROM table AS dummy1 LEFT JOIN table AS dummy2 ON dummy1.id = dummy2.subid WHERE dummy2.id IS NOT NULL Gives you the results as 1 cat 3 subcat 2 cat 4 subcat 2 cat 5 subcat Then you put it in an array while (list($id, $name, $subid, $subname) = mysql_fetch_row($res)) { $data[$name][] = $subname; } And loop it in the template {foreach from=$data item="cat" key="catname"} - {$catname} {foreach from=$cat item="subcatname"} -- {$subcatname} {/foreach} {/foreach} And you should change the value of nonexistent subids from 0 to NULL. (to use the very query from above) - david > My database structure is the simpliest one: > > +-----+--------+-------+ > | id | subid | name | > +-----+--------+-------+ > | 1 | 0 | cat | > +-----+--------+-------+ > | 2 | 0 | cat | > +-----+--------+-------+ > | 3 | 1 | subcat| > +-----+--------+-------+ > | 4 | 2 | subcat| > +-----+--------+-------+ > | 5 | 2 | subcat| > +-----+--------+-------+

Attachment: [application/pgp-signature]
« previous php.smarty.general (#11144) next »