Yesterday I was working with a site that allowed multiple levels of categories. A category could have subcategories, and that subcategory could have subcategories and so on and so on.

I needed a way to print a drop-down menu from the category structure. I won’t go into the database structure for this today – I’ll save that for another post. What I want to explain today is how I traverse that list to make a visual representation of that category and subcategory structure.

Since showing you exactly what I did with the categories would be a little complex for the example I’m trying to show, I want to show you the basic idea of recursion which is how I traversed the list of categories from my database.

For this example, I’m going to show you how to make the title for this tip – ‘Recur-Recur-Recursion-sion-sion’ – in code. As you can see the word recursion is nested in the middle of several other words recur- -sion. This is what I wanted to do with my categories. My list of top level categories had the subcategories nested between them. Each new subcategory was nested in between the upper level categories. So, basically, it’s the same recursive idea.

Since I could have an undetermined number of levels of subcategories, I want to pretend that I can have an undetermined number of the words ‘recursion’ nested in the word. I will use a variable to set the maximum number – in your code this number could be the number of categories or whatever.

To make recursion work in my code, I must have a stopping point. In this example, my stopping point is when I reach the set number of words I want to nest. When I reach that number I will print out the whole word recursion instead of breaking it in half. In my category example, my stopping place is the last subcategory of categories.

 subcategory 1 to category
  subcategory to subcategory to category

To make these categories and subcategories work in my HTML page, I would need to nest them in <li>tags, so hopefully, you can see the similarities in the two examples.

At some point, I will reach the end of the line. I will get to the innermost subcategory, or word ‘recursion’ in our example. I must use a condition to find out if I’m at that last word or that innermost subcategory.

In our example, the whole word ‘recursion’ is nested in the middle of all the others, I want to build the string instead of going ahead and printing out that word. In other words, to make this work correctly, I have to find the innermost part of the string I’m building first and then work my way out wrapping the rest of the nested strings around that innermost word. Then I can print the string as a whole. Hopefully, this will make more sense as we get further in the example.

So, basically, this is what I need to do:

function recursion($num)
  $max = 3;
  if ($num = $max) {
    return 'recursion';
  return 'Recur-'.recursion($num).'-sion';

echo recursion(1);

This will pass a 1 to the recursion function.

The recursion function will check to see if the number passed in is the max. If so, then it sends back the whole word. If not, then it increments the number passed in, calls itself to get the next level, and wraps the outermost level around the next level.

The function above should give you ‘Recur-Recur-Recursion-sion-sion’. Of course, setting the max number higher will cause the string to get longer and longer as it nests more and more words around the innermost word. Pretty cool, huh?

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top