Unlimited Nesting using Template-Lite

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sphenn
Forum Commoner
Posts: 48
Joined: Sun Jul 17, 2005 8:08 pm
Location: Winnipeg, MB

Unlimited Nesting using Template-Lite

Post by Sphenn »

Hi,

I'm trying to figure out a way to use Template-lite to display nested categories, however, there is no preset number of sub-categories. My code looks like this:

Code: Select all

//DB call here

while ( $row = mysql_fetch_assoc($result) )
{
   if ( empty($row['child']) )
   {
      $catInfo[$row['id']]['name'] = $row['name'];
      // etc for all the properties
   }
   else
   {
      $catInfo[$row['parent_id']][$row['id']]['name'] = $row['name'];
     // etc
   }
}
I'm unsure how to proceed from here. I don't know how to extend this to support unlimited sub-categories. As well, I'm not sure how I would template it.

Any thoughts or comments would be appreciated.

Thanks,

Sphenn
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The easiest thing to do would be to use the { section } taf in conjunction with the { if } tag within the template file (.tpl) if you are using Template Lite.

The beauty of the TL App is that you can leave the presentation logic up to TL. I just did this a few weeks ago with a two nest array read and it does with no problems.

Here is a brief snippet of a few different types of loops I am doing with a TL tpl file...

Code: Select all

	{foreach key=di value=div from=$divisions}
	<div class="relatedLinks"> 
		<h3>{ $div } Division</h3>
		<ul>
	{section name=ti loop=$teams}
		{if $teams[ti].team_division == $di}
			<li><a href="{$u_teams}/{ $teams[ti].team_domain_short }/" title="Get more information on the { $teams[ti].team_name }">{ $teams[ti].team_name }</a></li>
		{/if}
	{/section}
		</ul>
	</div>
	{/foreach}

	<div id="linkBox"> 
		<div class="relatedLinks"> 
			<h3>Stay Up To Speed</h3> 
			<ul>
			{section name=sidenav loop=$sidebar_links}
				{ if $templatelite.section.sidenav.index lt $trigger_sidebar_strong }
				<li class="highlight"><a href="{$sidebar_links[sidenav].link_url}" title="{$sidebar_links[sidenav].link_text}">{$sidebar_links[sidenav].link_text}</a></li>
				{ else }
				<li><a href="{$sidebar_links[sidenav].link_url}" title="{$sidebar_links[sidenav].link_text}">{$sidebar_links[sidenav].link_text}</a></li>
				{ /if }
				{/section}
			</ul> 
		</div> 
	</div> 
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Unlimited Nesting using Template-Lite

Post by Weirdan »

Sphenn wrote: I'm unsure how to proceed from here. I don't know how to extend this to support unlimited sub-categories. As well, I'm not sure how I would template it.

Any thoughts or comments would be appreciated.
I would represent it with an array with the following structure:

Code: Select all

array(
   array("item_name1", "nesting_level1"),
   array("item_name2", "nesting_level2"),
   // ....
   array("item_nameN", "nesting_levelN"),
);
and then template it with simple {section} loop. The problem here is that you need to build ordered and 'indented' list outside the template.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I think everything you want to do can be accomplished within the template. You can use the{ section } or the { foreach } tag to loop an array, then, inside of that loop, do an { if } to see if there is anything else to loop through for each array and level you go through.

To speed things up a bit you could use a counter within the code to tell you how may nests to make and then use that as a var in the template for generating the needed number of nests.
Post Reply