我正在使用此功能显示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"
}
}
答案 0 :(得分: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);
答案 1 :(得分:0)
您没有在此处打开要关闭的<li>
代码,
解决这个问题然后尝试,
else {
$html .= "</li>\n</ul>\n</li>\n";
$diff--;
}