帮助在列PHP MySQL中显示数据(修改的预订树遍历)
-
28-09-2020 - |
题
Hello我正在尝试获得多列显示(如果可以在此处调用SO),下面的代码是结果: 。有人可以看到为什么这些叶子被破坏了?或者告诉我,哪个更好:一个被链接到子模式的Catagory表,或邻接模型列表。
$stmt = $conn->prepare("SELECT node.idCat_ad ,node.".$cat_name.",(COUNT(parent.".$cat_name.") - 1) AS depth
FROM cat_ad AS node
CROSS JOIN cat_ad AS parent
WHERE node.left_node BETWEEN parent.left_node AND parent.right_node
GROUP BY node.idCat_ad
ORDER BY node.left_node");
$stmt->execute();
$treeArray = $stmt->fetchAll();
?>
<div class="column_in_categories">
<div class="menucategories"><ul>
<?php
$x = 0; //counter
$num_cols = 3; //3 colums
$num_rows = count($resTree); //num columns
$result = '';
$newArray =array_slice($resTree,1); //removing the root tree
foreach ($newArray as $currNode) {
if ($currNode['depth'] ==1) { //no links in root
$result .= '<li class="header '.$classSprites.'">' . $currNode[$cat_name] . '</li>';
} else{ //if child, we put a link
$result .= '<li class="subcat"><a href="#">' . $currNode[$cat_name] . '</a></li>';
}
$x++; // incrementing da counter
if ($x == ceil($num_rows / $num_cols)) { //if a colunm, we clause it and begin another
$result .= '</ul></div> <div class="menucategories"><ul><li style="display:none;"> </li>';
$x=0; //
}
}
$result .="</ul></div>";
print $result;
.
序列化版本链接文本
解决方案
替换
foreach ($newArray as $currNode) {
if ($currNode['depth'] ==1) { //no links in root
$result .= '<li class="header '.$classSprites.'">' . $currNode[$cat_name] . '</li>';
} else{ //if child, we put a link
$result .= '<li class="subcat"><a href="#">' . $currNode[$cat_name] . '</a></li>';
}
$x++; // incrementing da counter
if ($x == ceil($num_rows / $num_cols)) { //if a colunm, we clause it and begin another
$result .= '</ul></div> <div class="menucategories"><ul><li style="display:none;"> </li>';
$x=0; //
}
}
.
by
foreach ($newArray as $currNode) {
$x++; // incrementing da counter
if ($x >= ceil($num_rows / $num_cols) and $currNode['depth'] ==1) { //if a colunm, we clause it and begin another
$result .= '</ul></div> <div class="menucategories"><ul><li style="display:none;"> </li>';
$x=0; //
}
if ($currNode['depth'] ==1) { //no links in root
$result .= '<li class="header '.$classSprites.'">' . $currNode[$cat_name] . '</li>';
} else{ //if child, we put a link
$result .= '<li class="subcat"><a href="#">' . $currNode[$cat_name] . '</a></li>';
}
}
.
这将仅在新的头条启动时启动新列。
不隶属于 StackOverflow