0

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

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

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

4

2 回答 2

0

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

if ( $id == $sid ) {

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

$id = $_GET['id'];

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

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

这就是让您只看到类别的子集的原因,要查看所有类别,您不应该将 id 参数传递给 url。

于 2013-05-27T19:21:59.230 回答
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
?>
于 2013-05-28T01:59:57.483 回答