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!
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:
//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.
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...
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:
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.