我想从MySQL数据库创建菜单树。下面是我的PHP代码。我无法从数据库中获取数据。我已经显示了显示我当前的PHP代码的数据。我还展示了实际的数据库数据。
如何显示菜单树?
<?php require_once 'include/dbpdo.php'; ?>
<html>
<head>
<style>
</style>
</head>
<body>
<?php
$stmt = $pdo->query('SELECT * FROM `category` where `parent_id` = 0');
$stmt->execute();
?>
<ul>
<?php while($menu1 = $stmt->fetch()){ ?>
<li><a href="<?php echo $menu1['category_link'] . "\n"; ?>"><?php echo $menu1['product'] . "\n"; ?></a>
<?php $stmt1 = $pdo->prepare('SELECT * FROM category WHERE parent_id = ?');
$stmt1->execute([$menu1['id']]);
$stmt1->fetch(); ?>
<ul>
<?php while($menu2 = $stmt1->fetch()){ ?>
<li><a href="<?php echo $menu2['category_link'] . "\n"; ?>"><?php echo $menu2['product'] . "\n"; ?></a></li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
</body>
</html>
现在使用上面的php代码显示菜单树的数据:
实际数据如下:
从数据库中提取:
答案 0 :(得分:1)
您正在使用其他提取$stmt1->fetch(); ?>
,该提取会跳过第一个类别For Sale
<?php
require_once 'include/dbpdo.php';
?>
<html>
<head>
<style>
</style>
</head>
<body>
<?php
$stmt = $pdo->query('SELECT * FROM `category` where `parent_id` = 0');
$stmt->execute();
?>
<ul>
<?php while($menu1 = $stmt->fetch()){ ?>
<li><a href="<?php echo $menu1['category_link'] . "\n"; ?>"><?php echo $menu1['product'] . "\n"; ?></a>
<?php $stmt1 = $pdo->prepare('SELECT * FROM category WHERE parent_id = ?');
$stmt1->execute([$menu1['id']]);
?>
<ul>
<?php while($menu2 = $stmt1->fetch()){ ?>
<li><a href="<?php echo $menu2['category_link'] . "\n"; ?>"><?php echo $menu2['product'] . "\n"; ?></a></li>
<?php
$stmt2 = $pdo->prepare('SELECT * FROM category WHERE parent_id = ?');
$stmt2->execute([$menu2['id']]);
if($stmt1->rowCount()) echo "<ul>";
while($menu3 = $stmt2->fetch()){
echo "<li>".$menu3['product'] ."</li>";
}
if($stmt1->rowCount()) echo "</ul>";
?>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
</body>
</html>
答案 1 :(得分:1)
您可以使用此代码:
<?php require_once 'include/dbpdo.php'; ?>
<html>
<head>
<style>
</style>
</head>
<body>
<?php
$stmt = $pdo->query('SELECT * FROM `category` where `parent_id` = 0');
$stmt->execute();
$menues = $stmt->fetchAll();
?>
<ul>
<?php
foreach ($menues as $menuL1){
if ($menuL1['parent_id'] == 0) {
echo '<li><a href="' . $menuL1['category_link'] . '">' . $menuL1['product'] . '</a><ul>';
foreach ($menues as $menuL2) {
if ($menuL2['parent_id'] == $menuL1['id']) {
echo '<li><a href="' . $menuL2['category_link'] . '">' . $menuL2['product'] . '</a><ul>';
foreach ($menues as $menuL3) {
if ($menuL3['parent_id'] == $menuL2['id']) {
echo '<li><a href="' . $menuL3['category_link'] . '">' . $menuL3['product'] . '</a></li>';
}
}
echo '</ul></li>';
}
}
echo '</ul></li>';
}
}
?>
</ul>
</body>
</html>
答案 2 :(得分:1)
你也可以通过以下功能来实现:
<?php require_once 'include/dbpdo.php'; ?>
<html>
<head>
<style>
</style>
</head>
<body>
<?php
function getMenu($level) {
$stmt = $pdo->query('SELECT * FROM `category` where `parent_id` = ' . $level);
$stmt->execute();
return $stmt->fetchAll();
}
function child($parentId) {
$return = [];
$array = getMenu($parentId);
foreach ($array as $arr) {
if ($arr['parent_id'] == $parentId) {
$return[] = $arr;
}
}
return $return;
}
function listGen($parentId = 0) {
$html = '<ul>';
$array = child($parentId);
foreach ($array as $arr) {
$html .= '<li><a href="' . $arr['category_link'] . '">' . $arr['product'] . '</a>';
$html .= listGen($arr['id']);
$html .= '</li>';
}
$html .= '</ul>';
return $html;
}
echo listGen();
?>
</body>
</html>