|
Posted by Rik on 02/08/07 15:38
Hmmmz, did some testing just in case... here's the debugged code, and ma=
n =
can one get huge and weird structures if one's not carefull assigning =
what's a child of what :-)
<?php
$list =3D array(0 =3D> array('childs' =3D> array()));
$list_result =3D mysql_query('SELECT `id`, `name` FROM `category`');
while($row =3D mysql_fetch_assoc($list_result)){
$list[intval($row['id'])] =3D $row;
}
$relations_result =3D mysql_query('SELECT `parent_id`, `child_id` FROM =
`category_categories` ORDER BY `parent_id`');
while($row =3D mysql_fetch_assoc($relations_result)){
if(!isset($list[$row['parent_id']]['childs'])) =
$list[$row['parent_id']]['childs'] =3D array();
$list[$row['parent_id']]['childs'][] =3D& $list[$row['child_id']];
}
function html_nested_lists($array, $safety =3D 0){
if($safety =3D=3D 30){
trigger_error('html_nested_lists: nesting to deep, 30 levels allowed')=
;
return '';
}
$safety++;
if(!is_array($array) || empty($array)) return '';
$return =3D '<ul>';
foreach($array as $item){
$return .=3D '<li>'.$item['name'];
if(isset($item['childs'])) $return .=3D =
html_nested_lists($item['childs'],$safety);
$return .=3D '</li>';
}
return $return;
}
echo html_nested_lists($list[0]['childs']);
?>
-- =
Rik Wasmus
Navigation:
[Reply to this message]
|