Categories revisited

August 5, 2004 at 3:47 pm Leave a comment

I recently have revisited my category code to enhance it, as I found a few things lacking…

Since I allow multi-layer categories, it was difficult to visualize your sub-categories as there was very little to display the categories above the current one in the admin panel, or when selecting a category for a question… It was simply a list of categories, such as “Email, Webmail, Mail Server Settings”, etc.. Nevermind that “Email” was a main category, with “WebMail” and “Mail Server Settings” below it.

To fix this, I wrote a routine called createCategoryString, which would recursively build a string of Categories… So, in the above example, “WebMail” would be listed as “Email > WebMail”, etc… It worked great, but this caused problems of its own…

I now understand why most web based applications don’t offer multi-layered categories… The only way to display things properly is via Recursion… For those of you who don’t know the joys of Recursion, here’s a definition from a programming site I found:

Recursion: When a function called itself, either directly or indirectly. If this isn’t clear, refer to the entry for “recursion”.

What good is that, you might ask.. Well, you can write a good recursive function in very few lines and it will do much more than the number of lines might suggest. Here’s a very simple example:

function recursionTest ($var)
if ($var > 0)
return $var . “\n” . recursionTest($var-1);
return $var;
echo recursionTest(100);

The above example returns a webpage containing a countdown from 100, with one number per line… Yea, not terribly useful and this particular example can be more easily done with a loop, but hey – It’s just an example…

Recursion really excels when there’s math involved, or when you have to search through data, find one result, then search through it again, etc. until you don’t turn up any new results…

The issue that I ran into with recursion is that it is possible to change the way categories are linked so that a loop is created… Very simply, using the same examples as above, if “WebMail” is a sub-category of “Email”, then you allow the admin to make “Email” a sub-category of “WebMail”… Well, next time the recursive function runs, you have an infinite loop, or in PHP’s case, a loop that runs the full length of the “max_execution_time”.

How did I solve this issue? You might have guessed it.. More recursion, of course!

I wrote a routine which will determine all sub-categories of the current category.. (And all sub-categories of those sub-categories, etc.)… This is passed to the routine which generates the drop-down list of Categories for selecting a “Parent Category”… This list is treated as an exclusion list, so the drop-down list of categories that the customer can select from will automatically have “dangerous” options removed…

You know, the deeper that I get into this, the more I think that I should probably think about writing this into a class…


Entry filed under: PHP.

FAQ is coming along nicely Back to the FAQ

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


August 2004
« Jul   Sep »

Most Recent Posts

%d bloggers like this: