Page 2 of 2
Re: Admin Cats and Sub Cats
Posted: Thu Nov 13, 2008 7:48 pm
by aceconcepts
Once you have the database tables sorted as discussed, you will then need to program a script to get the categories and sub cats accordingly.
It will take the following structure:
1. Get/loop all categories
2. Output category names
3. Get/loop all sub cats associated with current category (2 above) in the loop
4. Output all sub cats retrieved from stage 3 above
5. End sub cat loop
6. End category loop
Does this make logical sense? I've indented 3 to 5 to help indicate the looping structure.
Re: Admin Cats and Sub Cats
Posted: Thu Nov 13, 2008 9:51 pm
by josh
That works for one level of sub category, what about two? You could add a third loop, but then adding a 3rd level?? Add a 4th loop? This starts to get out of hand.
Sounds like a good candidate for functional recursion which sounds daunting, but you can think of a metaphor where you had 2 mirrors that reflect each other, you effectively get "infinite" mirrors. Well in programming using recursion if you have a function call _itself_ you get the same effect. The only complicating factor is you need some stop condition, otherwise your function will just keep calling itself forever.
A revised psuedo code would be
Code: Select all
1. $current_category is ROOT
2. add $current_category to the list
3. $list = get immediate subordinate categories of $current_category
{ FOR EACH category in $list }
{ IF it has >= 1 sub-categories itself }
{ THAN set $current_category to this category and GOTO step 3 }
END { return $list }
This might translate to PHP code that looks like this:
Code: Select all
function getCategoriesOf( $category )
{
// some code
getCategoriesOf( $eachSubcategory ): // this is where the recursion happens.
// some other code
return $list;
}
$completeList = getCategoriesOf( $theRootCategory );
Re: Admin Cats and Sub Cats
Posted: Fri Nov 14, 2008 2:52 am
by aceconcepts
Indeed recursion is a good method for this rather laborious and/or convoluted multiple looping proceedure.
What would we do without a functions eh??? - we'd use includes hahaha

Re: Admin Cats and Sub Cats
Posted: Fri Nov 14, 2008 2:21 pm
by josh
aceconcepts wrote:we'd use includes hahaha

Basically what global variables simulates
/puke.
Re: Admin Cats and Sub Cats
Posted: Fri Nov 14, 2008 2:42 pm
by stewartrose
aceconcepts wrote:Once you have the database tables sorted as discussed, you will then need to program a script to get the categories and sub cats accordingly.
It will take the following structure:
1. Get/loop all categories
2. Output category names
3. Get/loop all sub cats associated with current category (2 above) in the loop
4. Output all sub cats retrieved from stage 3 above
5. End sub cat loop
6. End category loop
Does this make logical sense? I've indented 3 to 5 to help indicate the looping structure.
Yes this makes very good sence...
All the best from Alan