If you want tree-like structures with infinite depth, you need a recursion function. You also want to track the parent of each category, rather than all subcategories. I find it easier to refer to each point on the tree as a node, and set up a simple table like:
[FONT=courier new]nodeId | nodeParentId | nodeName[/FONT]
By identifying the parent of every node (category), you can start at any given node on the tree and walk through each child.
It's actually really easy to do this with an RDBMS and a single function.
function display_branch($node)
{
static $tab;
// Get all children of this node
$sql = "SELECT * FROM tblNodes WHERE nodeParentId = " . $node;
if($rs = $db->execute($sql)) // Get recordset
{
$oldtab = $tab; // Backup tab
$tab .= ' '; // Increase tab
while(!$rs->EOF)
{
echo $tab . $rs->fields['nodeName'] . '<br />'; // Output node name
node_tree($rs->fields['nodeId']); // Recurse with current node
$rs->MoveNext(); // Move to next record
}
$tab = $oldtab; // Restore tab
}
return true;
}
I haven't tested the above function but the idea is very similar. I'm not promising this is the most efficient method, but a good starting point. I use a custom DB and template class consistently so my DB nomenclature might be foreign, and you might not want to echo the output directly from this function.
The general idea is:
- Pass the function a node
- Get each child of that node
- Output each child node, passing each node back to the function to repeat the process
The $tab var just keeps track of your current tab indent. If this is just confusing you even more, I'll try to clarify when it isn't 5am. 😉
(Now to bed for three hours...)