gpt4 book ai didi

php - 使用 mysql 的带有子菜单的菜单

转载 作者:行者123 更新时间:2023-11-30 01:36:44 25 4
gpt4 key购买 nike

您好,我只是一点点,但可以提供帮助。

我想做的是通过从 2 个不同的 mysql 表获取数据来创建一个带有子菜单的菜单。

我遇到的问题是子菜单不会显示“子类别”数据库中的所有结果,具体取决于我所在的页面

表 1:类别“ID、名称”

表 2:子类别“ID、名称、Cparent、文件名(子类别的图像)”

我有以下代码:

<ul>
<?php
$catmenu_sql = 'select category.id AS catid, category.name AS catname, scategory.cparent AS scparent, scategory.name AS scname
from category
left join scategory on category.id = scategory.cparent
group by category.name'; // Select data from database
$result = mysql_query($catmenu_sql);

while($rows = mysql_fetch_array($result)) { ?>

<!-- Begin Category list -->
<li class="menu">
<a href="category.php?id=<?php echo $rows['catid']; ?>" id="<?php echo $rows['catid']; ?>" class="menu"><?php echo($rows['catname']); ?></a>
</li>
<!-- End Category List -->

<?php
if (isset($_GET['id']) && is_numeric($_GET['id'])) // get the 'id' variable from the URL and match it with scategory parent in database
$id = $_GET['id'];
$sid = $rows['scparent'];
if ( $id == $sid ) {
?>

<!-- Begin Sub Category List -->
<ul>
<li class="menu"><a href="scategory.php?id= <?php echo $rows['catid']; ?>" id=" <?php echo $rows['catid'];?>"class="smenu"><?php echo $rows['scname']; ?></a>
</li>
</ul>
<!-- End Sub category List -->
<?php }} ?>
</ul>'

任何帮助将不胜感激。谢谢

最佳答案

您可以尝试用此替换您的代码

<?php 

$catmenu_sql = 'select category.id AS catid, category.name AS catname, scategory.cparent AS scparent, scategory.name AS scname
from category
left join scategory on category.id = scategory.cparent
group by category.name'; // Select data from database

$result = mysql_query($catmenu_sql);


$sHTML="<ul>";

while($rows = mysql_fetch_assoc($result))
{

$sHTML="<ul>"

<!-- Begin Category list -->
$sHTML .= '<li class="menu">' .
'<a href="category.php?id=' . $rows['catid'] . ' id=' . $rows['catid'] . ' class="menu">' . $rows['catname'] .'</a>' .
'</li>' ;
<!-- End Category List -->


$subquery = "SELECT sec_name FROM tbl_user_sec WHERE `sec_group` = '" . mysql_real_escape_string($row_secs['sec_group'] . "'";
$subresult = mysql_query($subquery);

<!-- Begin Sub Category List -->
if (isset($_GET['id']) && is_numeric($_GET['id'])) // get the 'id' variable from the URL and match it with scategory parent in database
$id = $_GET['id'];
$sid = $rows['scparent'];
if ( $id == $sid )
{

$sHTML .="<ul>";

while($row = mysql_fetch_assoc($subresult) ) {

$sHTML .= '<li class="menu"><a href="scategory.php?id= ' . $rows['catid'] . ' id=' . $rows['catid'] . ' class="smenu">' . $rows['scname'] . '</a>' .
'</li>' ;
}

$sHTML .="</ul>";
}
<!-- End Sub category List -->


}

$sHTML .= '</ul>'
echo $sHTML
?>

关于php - 使用 mysql 的带有子菜单的菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16779233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com