Br@nkoR http://localhost
Član broj: 2597 Poruke: 1603
Sajt: localhost
|
Npr.
Code (php):
$items = array(
array(
'id' => 1,
'parent' => 0,
'title' => 'food',
),
array(
'id' => 2,
'parent' => 1,
'title' => 'fruit',
),
array(
'id' => 3,
'parent' => 2,
'title' => 'green',
),
array(
'id' => 4,
'parent' => 3,
'title' => 'pear',
),
array(
'id' => 5,
'parent' => 2,
'title' => 'red',
),
array(
'id' => 6,
'parent' => 5,
'title' => 'cherry',
),
array(
'id' => 7,
'parent' => 2,
'title' => 'yellow',
),
array(
'id' => 8,
'parent' => 7,
'title' => 'bannana',
),
array(
'id' => 9,
'parent' => 1,
'title' => 'meat',
),
array(
'id' => 10,
'parent' => 9,
'title' => 'beef',
),
array(
'id' => 11,
'parent' => 9,
'title' => 'pork',
)
);
function makeTree (&$items) {
$new = array();
foreach ($items as &$item) {
$new[$item['id']] = &$item;
$new[$item['parent']]['childs'][] = &$item;
}
return $new[0]['childs'];
}
$tree = makeTree ($items);
function printTree ($items) {
echo '<ul>';
foreach($items as $item) {
if(isset($item['childs']) AND count($item['childs']) > 0) {
echo '<li><a href="open.php?id='.$item['id'].'">' . $item['title'].'</a>';
printTree ($item['childs']);
echo '</li>';
} else {
echo '<li><a href="open.php?id='.$item['id'].'">' . $item['title'].'</a></li>';
}
}
echo '</ul>';
}
printTree ($tree);
[Ovu poruku je menjao Br@nkoR dana 23.04.2011. u 18:22 GMT+1]
Banned - Not available
|