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

From: Date: Wed, 17 Mar 2004 15:54:01 +0000
Subject: Re: [SMARTY] Re: this mysql query with smarty
References: 1 2 3 4  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-11140@lists.php.net to get a copy of this message
It is not trivial to traverse and display a tree of data with just a parent/child id. For one, there is no way to query your data in the correct order from the database (unless the db specifically supports this) in one query, it has to be unraveled. Then you have to figure out the depth (indent level) for each element. One way to overcome this (not perfect, but works) is to store the depth and order in the database with each element. This way they can be pulled with one select statement (order by) and the depth tells how to indent them in the display. The only downside to this approach is that if you insert a new element, the order must be updated (+1) for all elements following the new one. That can also be done with one query, but could feasibly have to update a lot of records in the case of a newsgroup spool or something equally large. Another method is using a serial id like so: AAA AAB AAC AACAAA AACAAB AACAACAAA AAD Each ID is three letters in my example, which limits you to 26^3 combinations for uppercase A-Z (if you need more, use more chars or positions). For a child node you tack on three new characters to the parent id. It is easy to see the parent/child relationship, and also easy to get the depth (serial length / 3). Its also easy to select immediate or all child elements for a given parent. The only problem with this approach is getting the info from the database in the correct order in one query. You can't just order by serial id, you get the short ones first. Maybe someone can give you a better idea, good luck. On Wed, 2004-03-17 at 09:20, dr. zoidberg wrote: > Dr. Zoidberg wrote: > > > > 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| > > +-----+--------+-------+ > > I'm googling for days but no solution. I manage to display categosries > and subcategories using simple mysql query and sort data in template. > Here is how my template looks like: > > {section name=i loop=$menu_data} > {if $menu_data[i][1] eq "1"} > id: {$menu_data[i][0]}<br> > {section name=ii loop=$menu_data} > {if $menu_data[ii][3] eq $menu_data[i][2] and $menu_data[ii][1] eq "0"} > id1: {$menu_data[ii][0]}<br> > {/if} > {/section} > {/if} > {/section} > > Is it OK to use this many conditions in templates? > > -- > Please do not CC me

« previous php.smarty.general (#11140) next »