0

我正在使用这个函数来显示 UL 列表:

function getList($arResult){
    $diff = 0;
    foreach($arResult as $result){
        if($lastlevel != $result[depth]){
        if($lastlevel < $result[depth]){
            $html .= "<ul>\n";
            $diff++;
        }
        else {
            $html .= "</li>\n</ul>\n</li>\n";
            $diff--;
        }       
        }
        else
        $html .= "</li>\n";

        $html .= "<li>$result[cat_name]";

        $lastlevel = $result[depth];
    }
    $html .= str_repeat("</li>\n</ul>\n", $diff);
    return $html;
    }

$arResult包含这样的树:

Electronics
  TV
  Video
    Old Movies
    New Movies
    Hollywood
      Old hollywood
      New HollyWood
      Rotten Hollywood
    Other Movies
  Mobile
    Nokia
    Apple
    Micromax
  Laptop
  Other

上面的函数打印下面的所有内容,Electronics->Tv而事实是下面Electronics应该有TV, Video, Mobile Laptop, Other..

这个函数不知何故进入ul li了不正确的模式。

数组转储在这里:

array(10) {
  [0]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "197"
    ["cat_name"]=>
    string(13) "Student Corner"
    ["cat_nicename"]=>
    string(13) "student-corder"
    ["parent"]=>
    string(1) "0"
    ["post_count"]=>
    string(1) "0"
    ["depth"]=>
    string(1) "0"
  }
  [1]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "198"
    ["cat_name"]=>
    string(6) "GujCET"
    ["cat_nicename"]=>
    string(13) "gujcet-gujrat"
    ["parent"]=>
    string(3) "197"
    ["post_count"]=>
    string(1) "0"
    ["depth"]=>
    string(1) "1"
  }
  [2]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "199"
    ["cat_name"]=>
    string(13) "Sample Papers"
    ["cat_nicename"]=>
    string(20) "sample-papers-gujcet"
    ["parent"]=>
    string(3) "198"
    ["post_count"]=>
    string(1) "1"
    ["depth"]=>
    string(1) "2"
  }
  [3]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "200"
    ["cat_name"]=>
    string(8) "Syllabus"
    ["cat_nicename"]=>
    string(15) "syllabus-gujcet"
    ["parent"]=>
    string(3) "198"
    ["post_count"]=>
    string(1) "8"
    ["depth"]=>
    string(1) "2"
  }
  [4]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "201"
    ["cat_name"]=>
    string(4) "News"
    ["cat_nicename"]=>
    string(11) "news-gujrat"
    ["parent"]=>
    string(3) "197"
    ["post_count"]=>
    string(1) "1"
    ["depth"]=>
    string(1) "1"
  }
  [5]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "202"
    ["cat_name"]=>
    string(13) "Question Bank"
    ["cat_nicename"]=>
    string(20) "question-bank-gujrat"
    ["parent"]=>
    string(3) "197"
    ["post_count"]=>
    string(1) "0"
    ["depth"]=>
    string(1) "1"
  }
  [6]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "203"
    ["cat_name"]=>
    string(7) "Class X"
    ["cat_nicename"]=>
    string(28) "class-x-gujrat-question-bank"
    ["parent"]=>
    string(3) "202"
    ["post_count"]=>
    string(1) "2"
    ["depth"]=>
    string(1) "2"
  }
  [7]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "204"
    ["cat_name"]=>
    string(9) "Class XII"
    ["cat_nicename"]=>
    string(30) "class-xii-gujrat-question-bank"
    ["parent"]=>
    string(3) "202"
    ["post_count"]=>
    string(1) "4"
    ["depth"]=>
    string(1) "2"
  }
  [8]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "205"
    ["cat_name"]=>
    string(7) "Results"
    ["cat_nicename"]=>
    string(15) "results-gujarat"
    ["parent"]=>
    string(3) "197"
    ["post_count"]=>
    string(1) "1"
    ["depth"]=>
    string(1) "1"
  }
  [9]=>
  array(6) {
    ["cat_ID"]=>
    string(3) "206"
    ["cat_name"]=>
    string(10) "About GSEB"
    ["cat_nicename"]=>
    string(10) "about-gseb"
    ["parent"]=>
    string(3) "197"
    ["post_count"]=>
    string(1) "1"
    ["depth"]=>
    string(1) "1"
  }
}
4

2 回答 2

1

功能太复杂了。沿着这些思路做了一些事情:

function treeList(array $data) {
    $list = '<ul>';

    foreach ($data as $item) {
        $list .= '<li>';
        $list .= $item['name'];
        if (!empty($item['children'])) {
            $list .= treeList($item['children']);
        }
        $list .= '</li>';
    }

    $list .= '</ul>';

    return $list;
}

$data = array(
    array(
        'name'     => 'Foo',
        'children' => array(
            array(
                'name' => 'Bar'
            )
        )
    )
);

echo treeList($data);
于 2012-10-15T10:37:46.013 回答
0

您没有在<li>此处打开要关闭的标签,请修复此问题并尝试,

 else {
        $html .= "</li>\n</ul>\n</li>\n";
        $diff--;
    }     
于 2012-10-15T10:37:25.193 回答