如何在php、mysql中制作多级菜单?
How to make multilevel menu in php, mysql?
我想在php中制作一个多级菜单,但它不起作用。有谁知道为什么下面的代码无法在 if close 中生成第一个 html 代码?
<?php
include "db.php";
if(isset($_POST["category"])){
$category_query="SELECT * FROM categories WHERE part='Product' ";
$run_query=mysqli_query($con,$category_query);
if(mysqli_num_rows($run_query)>0){
while($row=mysqli_fetch_array($run_query)){
$cat_id=$row["cat_id"];
$cat_name=$row["cat_name"];
for ($i=0; $i<count($cat_id); $i++) {
$category_query2="SELECT * FROM categories WHERE parent_id='$cat_id[$i]' ";
$run_query2=mysqli_query($con,$category_query2);
$j=0;
if(mysqli_num_rows($run_query2)>0){
$j++;
echo "
<li><a class='dropdown-button d' href='#' data-activates='dropdown2' data-hover=\"hover\" data-alignment=\"left\">$cat_name</a></li>
";
} else {
echo "
<li><a herf='2#'>$cat_name</a></li>
";
}
}
}
}
}
?>
我觉得代码应该改成这样。
if(mysqli_num_rows($run_query2)>0){`
$j++;
echo "HTML code 'attribute' ".$cat_name."HTML code";`
} else {`
echo "HTML code".$cat_name."HTML code";`
}
我想在php中制作一个多级菜单,但它不起作用。有谁知道为什么下面的代码无法在 if close 中生成第一个 html 代码?
<?php
include "db.php";
if(isset($_POST["category"])){
$category_query="SELECT * FROM categories WHERE part='Product' ";
$run_query=mysqli_query($con,$category_query);
if(mysqli_num_rows($run_query)>0){
while($row=mysqli_fetch_array($run_query)){
$cat_id=$row["cat_id"];
$cat_name=$row["cat_name"];
for ($i=0; $i<count($cat_id); $i++) {
$category_query2="SELECT * FROM categories WHERE parent_id='$cat_id[$i]' ";
$run_query2=mysqli_query($con,$category_query2);
$j=0;
if(mysqli_num_rows($run_query2)>0){
$j++;
echo "
<li><a class='dropdown-button d' href='#' data-activates='dropdown2' data-hover=\"hover\" data-alignment=\"left\">$cat_name</a></li>
";
} else {
echo "
<li><a herf='2#'>$cat_name</a></li>
";
}
}
}
}
}
?>
我觉得代码应该改成这样。
if(mysqli_num_rows($run_query2)>0){` $j++; echo "HTML code 'attribute' ".$cat_name."HTML code";` } else {` echo "HTML code".$cat_name."HTML code";` }