在我网站的第一页中,用户想要查看某些类别,每个类别下都有一些子类别。我从数据库中获取这些类别和子类别。数据库中有两个表称为" category"和"子类别"我试图合并这两个表。输出应该是这样的
但我得到了这个输出
这是我的代码
<?php
$host="localhost";
$user="root";
$pass="";
$db="doc";
$conn=mysqli_connect($host,$user,$pass,$db);
$show=mysqli_query($conn,"SELECT DISTINCT a.id, a.category_name, b.subcat_name, b.cat_name
FROM category a, subcategory b
WHERE a.category_name = b.cat_name");
?>
<html lang="en">
<head>
<title>Home</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css">
<link href="css/custom.css" rel="stylesheet">
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<script src="bootstrap/bootstrap.min.js"></script>
<script src="js/jquery.min.js"></script>
</head>
<body>
<div class="col-lg-3 sidebar">
<?php
while($showsub = mysqli_fetch_assoc($show)) { ?>
<ul>
<li><i class="fa fa-cube"></i><a href="category.php?id=<?php echo $showsub['id']; ?>"><?php echo $showsub['category_name']; ?></a>
<ul style="list-style-type:circle">
<li><?php echo $showsub['subcat_name']; ?></li>
</ul>
</li>
</ul>
<?php
}; ?>
</div>
</body>
</html>
我被困在这里过了两天。
答案 0 :(得分:1)
您应该首先相应地格式化数组 -
$categories= $subcats= array();
while($showsub = mysqli_fetch_assoc($show)) {
$categories[]= $showsub['category_name']; // array of categories
$subcats[$showsub['category_name']][]= $showsub['subcat_name']; // array of sub categories with category as indexes
}; ?>
然后是HTML -
foreach($categories as $cat) { // Loop through the categories array
?>
<ul>
<li><i class="fa fa-cube"></i><a href="category.php?id=<?php echo $showsub['id']; ?>"><?php echo $cat; ?></a>
<?php foreach($subcats[$cat] as $subcat) {?> // loop through the subcategories depending on categories
<ul style="list-style-type:circle">
<li><?php echo $subcat; ?></li>
</ul>
<?php } ?>
</li>
</ul>
<?php
}
答案 1 :(得分:0)
只有两个一般建议:学习一个模板系统,能够拆分演示文稿(html)和逻辑(php),并研究如何使用MySQL的预备语句 - 这将使你的生活更轻松一点点沿着这条路走下去
您获得的正是您所展示的代码所期望的内容。您要求每个子类别及其类别。你有两种不同的解决方法: 要么 - 正如Sougata所说 - 做两个不同的表并相应地加入。但也可以从一个表中创建两级列表。然后,您需要确保类别和子类别按照有意义的顺序排列:
$show=mysqli_query($conn,"SELECT DISTINCT a.id, a.category_name, b.subcat_name, b.cat_name
FROM category a, subcategory b
WHERE a.category_name = b.cat_name
order by category_name, subcat_name");
如果您只是添加&#34;按顺序排列&#34;对于您的SQL查询,您的输出应该看起来像
下一步是添加一些逻辑来压制重复的类别:
<ul>
<?php
$oldcat="";
$started=0;
while($showsub = mysqli_fetch_assoc($show)) {
$cat=$showsub['category_name'];
$subcat=$showsub['subcat_name'];
if ($oldcat != $cat){
if($started){print("</ul>");}
print("<li>$cat</li><ul>\n");
}
print("<li>$subcat</li>")
}
print("</ul></ul>\n");
?>
这不是您的代码的直接替代品,但我希望您明白这一点。把它读作一个php重的伪代码......
再次,请帮自己一个忙,并开始使用模板系统..
答案 2 :(得分:0)
<?php
$host="localhost";
$user="root";
$pass="";
$db="doc";
$conn=mysqli_connect($host,$user,$pass,$db);
?>
<html lang="en">
<head>
<title>Home</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css">
<link href="css/custom.css" rel="stylesheet">
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<script src="bootstrap/bootstrap.min.js"></script>
<script src="js/jquery.min.js"></script>
</head>
<body>
<div class="col-lg-3 sidebar">
<?php
$cat = mysqli_query($conn,"SELECT DISTINCT id, category_name FROM category");
while($rowCat = mysqli_fetch_assoc($cat))
{
$catName = $rowCat['category_name'];
?>
<ul>
<li>
<i class="fa fa-cube"></i>
<a href="category.php?id=<?php echo $rowCat['id']; ?>">
<?php echo $rowCat['category_name']; ?>
</a>
<?php
$subCat = mysqli_query($conn,"SELECT * FROM subcategory WHERE cat_name='$catName'");
while($rowSubCat = mysqli_fetch_assoc($subCat))
{?>
<ul style="list-style-type:circle">
<li><?php echo $rowSubCat['subcat_name']; ?></li>
</ul>
<?php }?>
</li>
</ul>
<?php
}?>
</div>
</body>
</html>