如何用PHP构建多级导航

如何用PHP构建多级导航,php,html,arrays,navigation,html-lists,Php,Html,Arrays,Navigation,Html Lists,我正在开发一个原型,并希望建立一个多层次的导航系统——但不是通过数组循环。我有一个$depth和一个$children,它们应该决定导航的深度和每个级别上的子级数量。因此,$depth=4,$children=8将产生4096个菜单项 这是我想要的输出的一个片段: <ul> <li class="level-1"> <a href="#">Subject 1</a> <ul>

我正在开发一个原型,并希望建立一个多层次的导航系统——但不是通过数组循环。我有一个$depth和一个$children,它们应该决定导航的深度和每个级别上的子级数量。因此,$depth=4,$children=8将产生4096个菜单项

这是我想要的输出的一个片段:

<ul>
    <li class="level-1">
        <a href="#">Subject 1</a>
        <ul>
            <li class="level-2">
                <a href="#">Subject 1.1</a>
                <ul>
                    <li class="level-3">
                        <a href="#">Subject 1.1.1</a>
                    </li>
                    ...
                </ul>
            </li>
            ...
        </ul>
    </li>
    ...
</ul>
      • ...
    • ...
  • ...
到目前为止,我已经试过了,但我无法理解:(

函数绘制列表($depth,$children){
回声“
    ”; 对于($i=0;$i<$children;$i++){ 回音“
  • ”($i++); $depth--; 如果($depth>0){ echo绘图列表($depth,$children); } 回音“
  • ”; } 回声“
”; }
在我看来,需要做几件事

  • $depth--;
    需要在
    for
    循环之外
  • 您两次递增
    $i
    ,一次在
    for
    语句中递增,然后在
    echo'
  • '($i++);
  • $depth检查提前停止了一次,因此请使用
    =
    ,而不仅仅是
    编辑,仔细想想,这是一个错误的语句)
那应该给你

function draw_list ($depth, $children) {  
  echo '<ul>';
  $depth--;
  for ($i = 0; $i < $children; $i++) {
    echo '<li>' . $i;
    if ($depth > 0) {
      echo draw_list($depth, $children);
    }
    echo '</li>';
  }
  echo '</ul>';
}
函数draw_list($depth,$children){
回声“
    ”; $depth--; 对于($i=0;$i<$children;$i++){ 回音“
  • ”.$i; 如果($depth>0){ echo绘图列表($depth,$children); } 回音“
  • ”; } 回声“
”; }
更新

要显示标高编号,请尝试将字符串值作为参数传递给

function draw_list ($depth, $children, $display=''){  
  echo '<ul>';
  $depth--;
  for ($i = 0; $i < $children; $i++) {
    echo '<li>' . $display . ($i + 1);
    if ($depth > 0) {
      echo draw_list($depth, $children, $display . ($i + 1) . '.');
    }
    echo '</li>';
  }
  echo '</ul>';
}
函数draw_list($depth,$children,$display=''){
回声“
    ”; $depth--; 对于($i=0;$i<$children;$i++){ 回显“
  • ”.$display($i+1); 如果($depth>0){ echo绘图列表($depth,$children,$display.($i+1)。”; } 回音“
  • ”; } 回声“
”; }
我最终做了以下工作:

function build_nav ($depth, $children, $levels = array()) {

    echo '<ul>';
    $depth--;
    for ($i = 0; $i < $children; $i++) {

        $levels[$depth] = $i+1;

        echo '<li>';
        echo 'Subject: ' . implode('.', $levels); 

        if ($depth > 0) {
            build_nav($depth, $children, $levels);
        }
        echo '</li>';
    } 
    echo '</ul>';
}
function build\u nav($depth,$children,$levels=array()){
回声“
    ”; $depth--; 对于($i=0;$i<$children;$i++){ $levels[$depth]=$i+1; 回音“
  • ”; 回显“主题:”。内爆(“.”,$levels); 如果($depth>0){ 构建导航($depth,$children,$levels); } 回音“
  • ”; } 回声“
”; }
谢谢!现在我需要知道如何打印级别编号(1.3.3.7等)。我必须正确地构建一个数组并内爆。有什么想法吗?@tolborg,添加字符串参数,在for循环中使用参数和子索引创建新的sting,然后在递归中作为参数传递。如果有效,则有效。但在我看来,数组设置在错误的级别,因为深度将减少而不是增加同样,你也让我意识到我的例子的值太低了
function build_nav ($depth, $children, $levels = array()) {

    echo '<ul>';
    $depth--;
    for ($i = 0; $i < $children; $i++) {

        $levels[$depth] = $i+1;

        echo '<li>';
        echo 'Subject: ' . implode('.', $levels); 

        if ($depth > 0) {
            build_nav($depth, $children, $levels);
        }
        echo '</li>';
    } 
    echo '</ul>';
}