动态菜单与动态页面:多级分类数据无法展示的技术求助
解决多级分类数据展示问题
看起来你现在的核心问题是菜单没传递二级、三级分类的ID参数,同时查询代码只固定过滤了一级分类,所以只能展示一级分类的数据。我来一步步帮你解决:
第一步:修改菜单链接,传递对应层级的分类ID
你的菜单需要根据分类层级,在href里传递对应的ID参数:
- 一级分类:只传
cat_id(保持你原来的逻辑即可) - 二级分类:同时传递一级分类ID(
cat_id)和二级分类ID(比如subcat_id) - 三级分类:传递一级、二级、三级分类ID(
cat_id、subcat_id、subsubcat_id)
假设你的category表用parent_id区分层级(比如parent_id=0是一级分类,parent_id等于一级分类ID的是二级,以此类推),菜单生成代码可以这样写:
// 生成一级分类菜单 $level1Query = $connection->query("SELECT cat_id, product FROM category WHERE parent_id = 0"); while ($level1Row = $level1Query->fetch_assoc()) { echo "<a href='slider1.php?cat_id={$level1Row['cat_id']}'>{$level1Row['product']}</a>"; // 生成二级分类菜单 $level2Query = $connection->query("SELECT cat_id, product FROM category WHERE parent_id = {$level1Row['cat_id']}"); while ($level2Row = $level2Query->fetch_assoc()) { echo "<a href='slider1.php?cat_id={$level1Row['cat_id']}&subcat_id={$level2Row['cat_id']}'>{$level2Row['product']}</a>"; // 生成三级分类菜单 $level3Query = $connection->query("SELECT cat_id, product FROM category WHERE parent_id = {$level2Row['cat_id']}"); while ($level3Row = $level3Query->fetch_assoc()) { echo "<a href='slider1.php?cat_id={$level1Row['cat_id']}&subcat_id={$level2Row['cat_id']}&subsubcat_id={$level3Row['cat_id']}'>{$level3Row['product']}</a>"; } } }
第二步:修改查询代码,动态过滤多级分类
你原来的代码只固定过滤了一级分类,现在需要根据传递的参数动态添加过滤条件,同时修复SQL注入风险(直接把变量拼进SQL是非常危险的行为):
<?php include('include/config.php'); // 接收各层级分类ID,默认设为null $level1 = isset($_GET['cat_id']) ? $_GET['cat_id'] : null; $level2 = isset($_GET['subcat_id']) ? $_GET['subcat_id'] : null; $level3 = isset($_GET['subsubcat_id']) ? $_GET['subsubcat_id'] : null; // 构建查询条件和参数 $conditions = []; $params = []; $paramTypes = ''; // 添加一级分类条件(如果有参数) if ($level1) { $conditions[] = "d.cat_id = ?"; $params[] = $level1; $paramTypes .= 'i'; // 假设cat_id是整数类型,根据你的表结构调整 } // 添加二级分类条件(如果有参数) if ($level2) { $conditions[] = "e.cat_id = ?"; $params[] = $level2; $paramTypes .= 'i'; } // 添加三级分类条件(如果有参数) if ($level3) { $conditions[] = "f.cat_id = ?"; $params[] = $level3; $paramTypes .= 'i'; } // 拼接WHERE子句 $whereClause = ''; if (!empty($conditions)) { $whereClause = "WHERE " . implode(" AND ", $conditions); } // 准备SQL语句 $sql = "SELECT a.img_id, a.img_type, a.img_size, b.ad_id, b.cus_id, b.ad_name, c.customer, c.mobile, d.product as category, e.product as subcategory, f.product as subcat FROM `image` a INNER JOIN `advt` b ON a.ad_id = b.ad_id INNER JOIN `customer` c ON b.cus_id = c.cus_id INNER JOIN `category` d ON b.category = d.cat_id INNER JOIN `category` e ON b.subcategory = e.cat_id INNER JOIN `category` f ON b.subcat = f.cat_id {$whereClause} GROUP BY b.ad_id"; if ($stmt = $connection->prepare($sql)) { // 绑定参数(如果有) if (!empty($params)) { $stmt->bind_param($paramTypes, ...$params); } $stmt->execute(); $stmt->store_result(); $stmt->bind_result($img_id, $img_type, $img_size, $ad_id, $cus_id, $ad_name, $customer, $mobile, $category, $subcategory, $subcat); while($stmt->fetch()){ ?> <tr class="example"> <td><?php echo $ad_id; ?></td> <td><?php echo $img_id; // 注意:你原代码里用了$img_name,但查询结果没有这个字段,这里改成了img_id,根据你的表结构调整 ?></td> <td><?php echo $cus_id; ?></td> <td><?php echo $ad_name; ?></td> <td><?php echo $customer; ?></td> <td><?php echo $mobile; ?></td> <td><?php echo $category; ?></td> <td><?php echo $subcategory; ?></td> <td><?php echo $subcat; ?></td> </tr> <?php } $stmt->close(); } ?>
关键说明
- 参数传递:通过菜单传递对应层级的分类ID,让后端明确需要过滤哪一级的数据
- 动态查询:根据接收到的参数动态构建WHERE条件,不管是一级、二级还是三级分类的请求,都能精准过滤
- 安全修复:使用
bind_param绑定参数,彻底避免SQL注入攻击(这是你原代码的一大安全隐患) - 字段修正:原代码中
<td><?php echo $img_name; ?></td>是错误的,因为你的查询没有返回img_name字段,我改成了img_id,你需要根据image表的实际字段调整
内容的提问来源于stack exchange,提问作者dipak dutta
相关产品推荐
相关产品推荐

