在php和mysql中,将鼠标悬停在类别名称上方,显示数据库中每个类别的子类别
在经历了这里提供的一些解决方案之后,我仍然无法正确地理解它。当我将鼠标悬停在类别名称上时,仅从数据库中提取并显示最后一个类别的子类别在php和mysql中,将鼠标悬停在类别名称上方,显示数据库中每个类别的子类别,php,html,mysql,sql,twitter-bootstrap,Php,Html,Mysql,Sql,Twitter Bootstrap,在经历了这里提供的一些解决方案之后,我仍然无法正确地理解它。当我将鼠标悬停在类别名称上时,仅从数据库中提取并显示最后一个类别的子类别 <div class="mega-menu-category"> <ul class="nav"> <!-- Other side-menu navs --> <?php $sql=mysqli_query($con,"S
<div class="mega-menu-category">
<ul class="nav">
<!-- Other side-menu navs -->
<?php $sql=mysqli_query($con,"SELECT id,categoryName FROM category");
while($row=mysqli_fetch_array($sql))
{
?>
<li><a href="category.php?cid=<?php echo $row['id'];?>"><?php echo $row['categoryName'];?></a>
<?php
$getid = $row['id'];
}?>
<div class="wrap-popup column1">
<div class="popup">
<?php $sql=mysqli_query($con,"SELECT id, subcategory FROM subcategory WHERE categoryid= '$getid'");
while($row=mysqli_fetch_array($sql))
{
?>
<ul class="nav">
<li><a href="sub-category.php?scid=<?php echo $row['id'];?>"><?php echo htmlentities($row['subcategory']);?></a></li>
</ul>
<?php } ?>
</div>
</div>
</li>
<!-- used one -->
</ul>
</div>
-
您正在使用外部查询的所有结果集,而不是在内部循环中使用它
下一个问题是对两个查询使用相同的变量,因为当运行内部查询时,它会破坏外部外观的结果集
<div class="mega-menu-category">
<ul class="nav">
<!-- Other side-menu navs -->
<?php
$sql=mysqli_query($con,"SELECT id,categoryName FROM category");
while($row=mysqli_fetch_array($sql)) {
?>
<li><a href="category.php?cid=<?php echo $row['id'];?>">
<?php echo $row['categoryName'];?>
</a>
<?php
//} this causes the first loop to run to its end hence only one category processed
?>
<div class="wrap-popup column1">
<div class="popup">
<?php
// use different variable otherwise you will destroy the first resultset
$sql1=mysqli_query($con,"SELECT id, subcategory
FROM subcategory
WHERE categoryid= '$row[id]'");
while($row1=mysqli_fetch_array($sql1)){
?>
<ul class="nav">
<li>
<a href="sub-category.php?scid=<?php echo $row['id'];?>">
<?php echo htmlentities($row1['subcategory']);?>
</a>
</li>
</ul>
<?php
} // end inner while
} // end outer while
?>
</div>
</div>
</li>
<!-- used one -->
</ul>
</div>
-
您正在使用外部查询的所有结果集,而不是在内部循环中使用它
下一个问题是对两个查询使用相同的变量,因为当运行内部查询时,它会破坏外部外观的结果集
<div class="mega-menu-category">
<ul class="nav">
<!-- Other side-menu navs -->
<?php
$sql=mysqli_query($con,"SELECT id,categoryName FROM category");
while($row=mysqli_fetch_array($sql)) {
?>
<li><a href="category.php?cid=<?php echo $row['id'];?>">
<?php echo $row['categoryName'];?>
</a>
<?php
//} this causes the first loop to run to its end hence only one category processed
?>
<div class="wrap-popup column1">
<div class="popup">
<?php
// use different variable otherwise you will destroy the first resultset
$sql1=mysqli_query($con,"SELECT id, subcategory
FROM subcategory
WHERE categoryid= '$row[id]'");
while($row1=mysqli_fetch_array($sql1)){
?>
<ul class="nav">
<li>
<a href="sub-category.php?scid=<?php echo $row['id'];?>">
<?php echo htmlentities($row1['subcategory']);?>
</a>
</li>
</ul>
<?php
} // end inner while
} // end outer while
?>
</div>
</div>
</li>
<!-- used one -->
</ul>
</div>
-
非常感谢@Riggs的愚蠢,这真的很神奇。我也很感谢这些资源。非常感谢@Riggs Folly,这真的很神奇。我也感谢这些资源。