I know this is asked a lot, but I can't get it to work correctly.
What I have A PHP function that get's the page id (fk_page). Inside this function the same function is called to look for child pages.
My Database looks like this:
The PHP Code looks like this:
private function createNav($parent = 0, $sub = false) {
// *!* Create Nav
$getNavPage = $this->model->loadNav($parent); // array of menu items (fk_page) that have $parent as parent.
$NavPage = 0;
foreach ($getNavPage as $getPage) {
$NavPage = intval($getPage["fk_page"]);
$subnav = $this->createNav($NavPage, true); // get childs (recursive loop) and save fk_page in $subnav
if($sub === false) $this->navArr[$parent][] = $NavPage;
else $this->navArr[$parent][][] = $NavPage;
}
return $NavPage;
}
The model does the following
public function loadNav($parent) {
$stmt = $this->pdo->prepare("SELECT fk_page FROM nav WHERE fk_parentpage = " . $parent . ";");
$stmt->execute();
return $stmt->fetchAll();
}
Now the result is an array that looks like this
array(3) {
[0]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
[2]=>
array(1) {
[0]=>
array(1) {
[0]=>
int(4)
}
}
[3]=>
array(2) {
[0]=>
array(1) {
[0]=>
int(5)
}
[1]=>
array(1) {
[0]=>
int(6)
}
}
}
What I would like to have as a result:
array(3) {
[0]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
array(1) {
[0]=>
array(1) {
[0]=>
int(4)
}
}
[3]=>
array(2) {
[0]=>
array(1) {
[0]=>
int(5)
}
[1]=>
array(1) {
[0]=>
int(6)
}
}
}
}
I believe that the createNav() must run before the array element is written (or even inside the array $this->NavArr), but I wasn't really succesfull.
