我已经创建了一个数据来自数据库的菜单。我需要看到第二级和第三级菜单,但问题是子菜单没有显示。只显示第一级。
我添加了css和php代码。
Plz check菜单图片:
php代码:
<?php
function display_children($parent, $level) {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "nfs";
$conn = new mysqli($servername, $username, $password, $dbname);
$result = mysqli_query($conn, "SELECT a.cat_id, a.product, a.category_link, Deriv1.Count FROM `category` a LEFT OUTER JOIN (SELECT parent_id, COUNT(*) AS Count FROM `category` GROUP BY parent_id) Deriv1 ON a.cat_id = Deriv1.parent_id WHERE a.parent_id=" . $parent);
echo "<nav><ul class='nav'>";
while ($row = mysqli_fetch_assoc($result)) {
if ($row['Count'] > 0) {
echo "<li><a href='" . $row['category_link'] . "'>" . $row['product'] . "</a>";
display_children($row['cat_id'], $level + 1);
echo "</li>";
} elseif ($row['Count']==0) {
echo "<li><a href='" . $row['category_link'] . "'>" . $row['product'] . "</a></li>";
} else;
}
echo "</ul><nav>";
}
echo display_children(0, 1);
?>
答案 0 :(得分:0)
您的if语句不正确。 使用此函数计算行数。 $ count = mysqli_num_rows($ result); if($ count&gt; 0){ .............. }