It is my understanding that recursive functions have been removed from the newer versions of PHP, at least as it relates to a heirarchical database structure. Anybody out there have success in pulling all of the subordinate items for a given main item, such that the subordinates can be used as variables later in the code?
Example (an online store with categories as listed below):
Electronics
..Computers
....Desktops
......PC
......Mac
....Laptops
..Televisions
....CRT
....LCD
....Plasma
Is there an easy way to say "I want everything subordinate to Computers," and end up with:
Computers
Desktops
PC
Mac
Laptops?
Recursive functions for heirarchical table structure
Moderator: General Moderators
Are you wanting to do recursive functionality in an SQL query? If that's the case, it doesn't matter what version of PHP you use, as you're just throwing queries at the DB. What you want to do is entirely possible, with the correct database structure.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
This is an interesting question, but I don't see how this would ever be a limitation of PHP... It looks like there may have been talk about MySQL Stored Procedure recursion being in and out of releases but I'm not sure...
A pretty good explaination for how to do what you're talking about: http://dev.mysql.com/tech-resources/art ... -data.html , http://www.sitepoint.com/article/hierar ... database/2 , and http://groups.google.com/group/comp.dat ... F-8&rnum=5
Anybody else know better ways to do this?
A pretty good explaination for how to do what you're talking about: http://dev.mysql.com/tech-resources/art ... -data.html , http://www.sitepoint.com/article/hierar ... database/2 , and http://groups.google.com/group/comp.dat ... F-8&rnum=5
Anybody else know better ways to do this?
There was nothing removed from PHP in relation to recursion. Recursion isn't a feature anyway. It's an algorithm that you employ when needed. The hopeful thing is that whatever language you use doesn't have a problem doing it.
Now there was a problem with recursive routines that required tons of memory in PHP (But that could be a problem in any language). There was also talk of limiting the engines depth of recursion, but I can't say with certainty what became of that.
Anyway, using SQL to do this would be a pain in the arse. I don't recommend it. The website that I maintain has a heirchal menu structure. I store all of the categories and sub categories IN THE SAME TABLE. Each category has a parent_id. If the parent_id of any category is 0, then it's a top level or root level category. It has no parents itself, but may have child nodes. Going further, each of those child nodes may have children as well.
Sooo......, in this way, I just pull all of the items out of my menu table into an array that is processed by a recursive function ( in PHP of course
) that organizes the heirarchy based on those parent_id's. When it encounters a node that has children, it recurses.
Now that's the basic logic right there.
And remember that trees (heirarchies) can be simulated with arrays in PHP.
I would suggest looking up recursion in PHP without using a database.
Cheers
Now there was a problem with recursive routines that required tons of memory in PHP (But that could be a problem in any language). There was also talk of limiting the engines depth of recursion, but I can't say with certainty what became of that.
Anyway, using SQL to do this would be a pain in the arse. I don't recommend it. The website that I maintain has a heirchal menu structure. I store all of the categories and sub categories IN THE SAME TABLE. Each category has a parent_id. If the parent_id of any category is 0, then it's a top level or root level category. It has no parents itself, but may have child nodes. Going further, each of those child nodes may have children as well.
Sooo......, in this way, I just pull all of the items out of my menu table into an array that is processed by a recursive function ( in PHP of course
Now that's the basic logic right there.
And remember that trees (heirarchies) can be simulated with arrays in PHP.
I would suggest looking up recursion in PHP without using a database.
Cheers