array (
array(
['name'] => 'test1',
['level'] => 1
},
array(
['name'] => 'test2',
['level'] => 2
},
array(
['name'] => 'test3',
['level'] => 2
},
array(
['name'] => 'test4',
['level'] => 3
},
array(
['name'] => 'test5',
['level'] => 3
},
array(
['name'] => 'test6',
['level'] => 3
},
array(
['name'] => 'test7',
['level'] => 3
},
)
I have an array of arrays sorted by level, thus I want to make something like this:
________
| |
| test1 |
|________|
________ ________
| | | |
| test2 | | test3 |
|________| |________|
________ ________ ________ ________
| | | | | | | |
| test4 | | test5 | | test6 | | test7 |
|________| |________| |________| |________|
render them like a pyramide each row by level, but first how do I render it by using recursion or using a while loop? to make each level wrapped by <ul>
and the nodes by <li>
I'am trying to render it by loop, but I don't know how, what is the logic?:
while ($item = $this->dbtree->NextRow()) {
$rows[] = (object)$item;
}
print_r($res); // list of arrays like the above
exit;
thanks for attention
You can first iterate over the array and sort them in to the levels in your application logic:
foreach ($res as $resValue){
if (!isset($levelSorted[$resValue['level'])){
$levelSorted[$resValue['level'] = array();
}
$levelSorted[$resValue['level'][] = $resValue;
}
In your view you can now do something like this:
foreach ($levelSorted as $level => $levelRes){
echo '<ul id="level' . $level . '">';
foreach ($levelRes as $singleRes){
echo '<li>' . $singleRes['name'] . '</li>';
}
echo '</ul>';
}
At the end, you have more than one loop, but it would be consistent with the MVC pattern.