So as an example I have this structure with pages in a website. The thing is I have to get these items in a dropdown list, but they have to intend based on their parent.
- loadSubItems(id) returns the subitems for the id.
- loadSubItems(0) will give the subitems from the root, which in this case is 9, 16, 20
- loadSubItems(9) will give an empty array loadSubItems(16) will return 17, 18, 19
The problem is that the middle loop (the foreach) has to keep on looping as long as there are subitems for that level, print them and for each level an extra intend "_" has to be added.
I have been trying to solve this for hours and I've lost my thinking and I can't seem to figure out what has to happen to be able to do this.
Any help is welcome! Thanks in advance:
Testdata below:
I'm using the following code to go to the second level:
$items = loadSubItems(0); // returns 9 16 20
$intend = "";
foreach($items as $item) {
$subitems = loadSubItems($item);
if(count($subitems) > 0) {
echo $intend.$item." has subitems: <br />";
$intend .= "_";
foreach($subitems as $subitem) {
echo $intend.$subitem."<br />";
}
} else {
echo "<br />".$item." has no subitems <br />";
}
}
This code has output:
9 has no subitems
16 has subitems:
_17
_18
_19
20 has no subitems
edit: So what I want as a result would be:
9 has no subitems 16 has subitems: _17 __21 ___22 __23 ___24 _18 _19 20 has no subitems