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.
Admin Cats and Sub Cats
Moderator: General Moderators
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: Admin Cats and Sub Cats
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
This might translate to PHP code that looks like this:
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 }
Code: Select all
function getCategoriesOf( $category )
{
// some code
getCategoriesOf( $eachSubcategory ): // this is where the recursion happens.
// some other code
return $list;
}
$completeList = getCategoriesOf( $theRootCategory );
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: Admin Cats and Sub Cats
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
What would we do without a functions eh??? - we'd use includes hahaha
Re: Admin Cats and Sub Cats
Basically what global variables simulatesaceconcepts wrote:we'd use includes hahaha
/puke.
-
stewartrose
- Forum Newbie
- Posts: 10
- Joined: Wed Nov 12, 2008 12:35 am
Re: Admin Cats and Sub Cats
Yes this makes very good sence...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.
All the best from Alan