Re: Unlimited nested sections

From: Date: Tue, 29 Jan 2002 01:14:15 +0000
Subject: Re: Unlimited nested sections
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-2587@lists.php.net to get a copy of this message
I'd like to point out that you have a recursive data relationship. In order for the code to work with an infinite number of nested categories, you need a recursive function to dig down and find the child categories of each root category. I have a catalog system I have developed (that I'm now porting to smarty) and this is how I solved the problem. The solution below works for the display, but may get sticky if you wanted to display the "path" to a product in the catalog like: Home -> Storage -> Hard Drive -> IDE I'm not trying to say this is wrong, only that you may want to consider how many levels of nesting you will have. This is one area where I chose to embed a minor amount of html in my php. Because the template got ugly. I now just have one variable with the whole string in it. Hope I've been of some help. Steve -----Original Message----- From: Linus McCabe [mailto:Linus@McCabe.nu] Sent: Sunday, January 27, 2002 10:01 AM To: smarty@lists.ispi.net Subject: [smarty] Re: Unlimited nested sections Fill your array with all category levels, something like this: $cats[0]['name'] = 'Category 1' $cats[0]['level'] = 1 $cats[0]['id'] = 12 $cats[1]['name'] = 'Category 1a' $cats[1]['level'] = 2 $cats[1]['id'] = 18 $cats[2]['name'] = 'Category 1b' $cats[2]['level'] = 2 $cats[2]['id'] = 22 $cats[3]['name'] = 'Category 1b(i)' $cats[3]['level'] = 3 $cats[3]['id'] = 42 $cats[4]['name'] = 'Category 2' $cats[4]['level'] = 1 $cats[4]['id'] = 43 (in reality this might be a recursive function, or somthing similar) Then loop something like: {section name=cat loop=$cats} <div style="margin-left: {$cats[cat].level}cm"> {$cats[cat].name} </div> {/section} /linus On Sat, 26 Jan 2002 15:36:54 -0500 Brad Atkins <brad@youreshop.com> wrote: > Hello, > > We have the following situation which is causing us lots of headaches, > and hope that someone can help us. > > In our application we want the ability to display an unlimited number of > categories and they can be nested within each. An example would be: > > Category 1 > ---Category 1a > ------Category 1a(i) > Category 2 > > This means that Category 1 has a sub category called Category 1a, and > Category 1a has a sub category called Category 1a(i), and this can > continue to any depths. > > We were initially using the {section} loops, however, we would have to > embed them within each other, and this gets messy. > > Is there some automated way to loop through these without having to > embed sections upon sections upon sections? > > Any help or ideas would be greatly appreciated! > > Thanks, > Brad > > > --- > You are currently subscribed to smarty as: linus@mccabe.nu > To unsubscribe send a blank email to $subst('Email.Unsub') > Visit Smarty's homepage at > http://www.phpinsider.com/php/code/Smarty/ > --- You are currently subscribed to smarty as: lists@iwsys.com To unsubscribe send a blank email to $subst('Email.Unsub') Visit Smarty's homepage at http://www.phpinsider.com/php/code/Smarty/

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