使用mysql的子菜单菜单

时间:2013-05-27 19:14:23

标签: php mysql submenu

你好,我只是一点点但是帮助。

我要做的是通过从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>'

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

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,这一行:

if ( $id == $sid ) {

根据您所在的网页过滤子类别,因为$id正在初始化为:

$id = $_GET['id'];

这是传递给url的名为id的参数的值,可能是这样的:

http://www.site.com/script.php?id=1

这就是让你只看到一组子类的东西,看看它们你不应该把id参数传递给url。

答案 1 :(得分:0)

您可以尝试使用此

替换您的代码
<?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
?>