3 回答

TA贡献1841条经验 获得超3个赞
由于您menu和sub-menu在不同的表中,您可以menu先选择,然后sub-menu根据所选菜单进行选择。即:
<?php
//getting menu first
$sql = 'SELECT id,intitule FROM rubriques';
$stmt = $conn->query($sql);
if ($stmt->num_rows > 0) {
while($row = $stmt->fetch_assoc()){
//getting values
$intitule = $row['intitule '];
$idr = $row['id']; ?>
<!--your menu-->
<li class="active"><a href="index.html"><?php
echo $intitule;
?></a>
<?php
//passing the id from first to action table for compare and retrieve that rows only
$sql1 = 'SELECT * FROM actions where idr= ' . $idr;
$stmt1 = $conn->query($sql1);
?>
<ul class="dropdown">
<?php
while ($row1 = $stmt->fetch_assoc()) {
$lien = $row1['lien'];
$intitulee = $row1['intitulee'];
?>
<!--your submenu-->
<li><a href="<?php echo $lien;?>"><?php echo $intitulee; ?></a></li>
<?php
}
?>
</ul> <!--closing ul -->
</li>
<?php
}//closing while
} //closing if
?>

TA贡献1876条经验 获得超7个赞
最终代码
<?php
//getting menu first
$sql = 'SELECT id,intitule FROM rubriques ';
$stmt = $conn->query($sql);
if ($stmt->num_rows > 0) {
while ($row = $stmt->fetch_assoc()){
//getting values
$intitule =$row['intitule'];
$id = $row['id']; ?>
<!--your menu-->
<li class="active"><a href="index.html"><?php
echo $intitule;
?></a>
<?php
//passing the id from first to action table for compare and retrieve that rows only
$sql1 = 'SELECT * FROM actions where idr= ' . $id;
$stmt1 = $conn->query($sql1);
?>
<ul class="dropdown">
<?php
while ($row1 = $stmt1->fetch_assoc()) {
$lien = $row1['lien'];
$intitulee = $row1['intitulee'];
?>
<!--your submenu-->
<li><a href="<?php echo $lien;?>"><?php echo $intitulee; ?></a></li>
<?php
}
?>
</ul> <!--closing ul -->
</li>
<?php
}}
$conn->close();
//closing if
?>

TA贡献1794条经验 获得超7个赞
看到这样写的查询会更常见:
$sql = "
SELECT r.id
, r.intitule
, a.intitulee
, a.lien
, a.idr
FROM rubriques r
JOIN actions a
ON a.idr = r.id
ORDER
BY r.id;
";
- 3 回答
- 0 关注
- 165 浏览
添加回答
举报