Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help traversing a recursion method tree 1

Status
Not open for further replies.

Itshim

Programmer
Apr 6, 2004
277
US
I am having an issue with one final piece of logic for an administration app. I’m creating for a search engine. The links within the search engine are categorized (and subcategorized).

The category tree is stored in the database using (what I have found to be called) the recursion method. Tables are as follows:

Table: Category
| Category_Id | | Category|

Table: Category_Subcategory
| Parent_Id | | Child_Id |

Table: Site_Category
| Site_Id | | Category_Id |

I understand that the recursion method is not the best way to store hieratical data, but it is what I currently have, anyway…

In one of the interfaces I need to know if a category (or one of its subcategories) has a site within it, so I know whether to make it a link in the interface. My problem comes in when I try to figure out the logic to use to check for sites. I have written a piece of code which will check the category, and if no site is ‘in’ that category it will then check its subcategories for sites. My problem is that I need a way for the code to continue traveling down the subcategories (of subcategories) until a site is found, or end when there are no other subcategories to check.

Since I want the code to stop as soon as a site is found I figure I need to place the entire code within a while loop, set a variable to 0 and use that variable to check against in the setup of the while loop, but I cannot figure out how to get code to dynamically traverse all the subcategories of a subcategory, since I will have no idea of how many levels of subcategories are within a category.

Ok, just trying to explain the problem has confused me, so if this doesn’t make sense please let me know, and I will try to explain it better.

Any help will be appreciated.

Thanks,
Itshim
 
I'm not sure if this will help, but something I forgot to mention in my first post, I already have two functions built. One to obtain the subcategories of the category passed to it, and another to return the path of a category working up the tree. I figure that either one or the other will need to be used.

I have considered first retrieving all the subcategories (and subcategories of subcategories) of a category, placing them into an array and then looping through that array to check for sites, but I run into the same problem where I cannot figure out a way to get all subcategories, subcategories of sub... of a category.

Thanks again,
Itshim
 
Are you at all familiar with recursive programming? PHP supports it.

A function, when handed a category id, will attempt to fetch items in that category. If none exist, the function will call itself once for each subcategory.

The function keeps calling itself until it runs out of things to process (a category has no items or subcategories), in which it returns FALSE, or it finds an item, in which case it would then return the id of the item.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I was unfamiliar with recursive programming, (I guess that would really help, huh?). Since reading your post, I have found several examples of recursive functions using PHP.

I believe I know how to program the function you described, which is exactly what I'm looking for. I will try writing the function and if I run into problems, I will probably be back :).

Thanks for the information.

Itshim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top