Chroder
New Member
[RESOLVED]
Any help appreciated
Visual Diagram
PHP:
Cats and Sub Cats[/b]
I have my database, and my database holds categories. These categories can hold sub-cats, and the sub-cats can hold more sub-cats etc.
Look at the [url=http://chris.devbox.net/diagram.gif]attachment[/url] to see a visual on what I have - I spend quite a while making it :P Anyway, my current code produces the snip on the right, what I need my code to make is the code on the left.
Here's what I've got.
[php]<?php
define('ROOT_DIR', './');
include(ROOT_DIR.'includes/global.php');
$sql = "SELECT * FROM ".TP."categories ORDER BY title ASC";
$cats = $db->getAll($sql);
foreach($cats as $key => $val) {
if($val['parent_id'] == 0) {
echo '<b>'.$val['title'].'</b><br />';
$id = $val['category_id'];
unset($cats[$key]);
getSubs($id, $cats);
}
}
function getSubs($id, &$cats)
{
if($id < 1)
return;
static $count;
if(!isset($count))
$count = 1;
$tab = '';
for($i = 0; $i < $count; $i++) {
$tab .= ' ';
}
foreach($cats as $key => $val) {
if($val['parent_id'] == $id) {
echo $tab.$val['title'].'<br />';
$sid = $val['category_id'];
unset($cats[$key]);
$count++;
getSubs($sid, $cats);
$count--;
}
}
}
?>
Any help appreciated
Visual Diagram
Last edited: