Admin Cats and Sub Cats

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

User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Admin Cats and Sub Cats

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Admin Cats and Sub Cats

Post 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 );
 
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Admin Cats and Sub Cats

Post 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 :wink:
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Admin Cats and Sub Cats

Post by josh »

aceconcepts wrote:we'd use includes hahaha :wink:
Basically what global variables simulates
/puke.
stewartrose
Forum Newbie
Posts: 10
Joined: Wed Nov 12, 2008 12:35 am

Re: Admin Cats and Sub Cats

Post 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
Post Reply