Eftersom jeg ikke gad lave en table er dette her lavet til et array defineret i koden, der er dog en udkommenteret while loop i bunden og du burde bare kunne gøre hvad kommentaren siger over dette loop.
<?
$row=array();
$i=0;
$row[$i++]=array('id'=>1,'catid'=>0,'name'=>'Mus');
$row[$i++]=array('id'=>3,'catid'=>0,'name'=>'Teflon');
$row[$i++]=array('id'=>2,'catid'=>1,'name'=>'Logitech');
$row[$i++]=array('id'=>5,'catid'=>1,'name'=>'Microsoft');
$row[$i++]=array('id'=>4,'catid'=>3,'name'=>'Black Ice');
$row[$i++]=array('id'=>6,'catid'=>4,'name'=>'1m');
$row[$i++]=array('id'=>7,'catid'=>4,'name'=>'10m');
$row[$i++]=array('id'=>8,'catid'=>4,'name'=>'30m');
$tree=array();
function bind($aTup,&$aTree){
for($j=0;$j<count($aTree);$j++){
if($aTup['catid']==$aTree[$j]['id']){
$aTup['childs']=array();
array_push($aTree[$j]['childs'],$aTup);
return true;
}else{
if(count($aTree[$j]['childs'])>0&&bind($aTup,$aTree[$j]['childs'])){
return true;
}
}
}
return false;
}
function printTree($atree,$abranch,$astem){
for($i=0;$i<count($atree);$i++){
print($astem.$atree[$i]['name'].'</br>');
if(count($atree[$i]['childs']>0)){
printTree($atree[$i]['childs'],$abranch,$astem.$atree[$i]['name'].$abranch);
}
}
}
for($i=0;$i<count($row);$i++){
if($row[$i]['catid']==0){
$row[$i]['childs']=array();
array_push($tree,$row[$i]);
}else{
bind($row[$i],$tree);
}
}
//uncomment while loop below and delete the for loop above and the $row inition at the top.
/*
//connect to db
$result=mysql_query(SELECT * FROM cats ORDER BY catid);
//disconnect from db
while($row=mysql_fetch_array($result)){
if($row['catid']==0){
$row['childs']=array();
array_push($tree,$row);
}else{
bind($row,$tree);
}
}
*/
printTree($tree,' > ','');
?>