简体   繁体   English

使用PHP的递归嵌套导航

[英]Recursive Nested Navigation with PHP

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). 我所拥有的PHP函数可以获取页面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: PHP代码如下所示:

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. 我相信createNav()必须在写入数组元素之前运行(甚至在$ this-> NavArr数组内部),但是我并没有真正成功。

Here is an example that uses local data but mimics what you're trying to do: 这是一个使用本地数据但模仿您要执行的操作的示例:

$data_array = array(
    0 => array(1, 2, 3)
    , 2 => array(4)
    , 3 => array(5, 6)
);

function get_kids($id = 0) {
    global $data_array;
    if(isset($data_array[$id])) {
        return $data_array[$id];
    }
    else {
        return array();
    }
}

function create_navigation($parent_id = 0) {

    // Check for children
    $children_array = get_kids($parent_id);

    // No children - just return the parent
    if (empty($children_array)) {
        return $parent_id;
    }

    // Children!
    foreach ($children_array as $key => $child_id) {
        $children_array[$key] = create_navigation($child_id);
    }

    return array($parent_id => $children_array);
}

echo '<pre>';
    $nav = create_navigation(0);
    var_dump($nav);
    print_r($nav);
echo '</pre>';

The $data_array is used instead of your database. 使用$data_array代替您的数据库。

The get_kids function is your loadNav method. get_kids函数是您的loadNav方法。

The create_navigation function is your createNav method. create_navigation函数是您的createNav方法。

This produces a var_dump : 这将产生一个var_dump

array(1) {
  [0]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    array(1) {
      [2]=>
      array(1) {
        [0]=>
        int(4)
      }
    }
    [2]=>
    array(1) {
      [3]=>
      array(2) {
        [0]=>
        int(5)
        [1]=>
        int(6)
      }
    }
  }
}

And a print_r : 还有一个print_r

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => Array
                (
                    [2] => Array
                        (
                            [0] => 4
                        )

                )

            [2] => Array
                (
                    [3] => Array
                        (
                            [0] => 5
                            [1] => 6
                        )

                )

        )

)

The problem with your original code is you were assigning IDs to the class variable $this->navArr instead of using recursion to return the child elements ( $subnav ). 原始代码的问题是,您正在向类变量$this->navArr分配ID,而不是使用递归返回子元素( $subnav )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM