简体   繁体   English

菜单与子菜单使用MySQL

[英]menu with submenu using mysql

Hello I just a little but help. 你好,我只是有点帮助。

What I am trying to do is create a menu with a submenu by getting the data from 2 different mysql tables. 我正在尝试通过从2个不同的mysql表获取数据来创建带有子菜单的菜单。

The issue I am having is the submenu will not display all results from the "Sub Category" database depending on what page I am on 我遇到的问题是子菜单将不会显示“子类别”数据库中的所有结果,具体取决于我所在的页面

Table 1: Category "ID, Name" 表1:类别“ ID,名称”

Table 2: Sub Category "ID, Name, Cparent, filename (Image for sub category)" 表2:子类别“ ID,名称,父代,文件名(子类别的图像)”

I have the following code: 我有以下代码:

<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>'

Any help will be appreciated. 任何帮助将不胜感激。 Thank you 谢谢

If i understand your problem correctly, this line: 如果我正确理解您的问题,请执行以下操作:

if ( $id == $sid ) {

filters the sub categories depending of the page you're, given that $id is being initialized as: 假设$id初始化为,则根据您的页面过滤子类别:

$id = $_GET['id'];

which is the value of a parameter named id that is being passed to the url, possibly like this: 这是传递给url的名为id的参数的值,可能是这样的:

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

that's what making you see only a sub set of the categories, to see them all you should not pass the id parameter to the url. 这就是让您仅看到类别的子集的原因,要查看所有类别,您不应将id参数传递给url。

you can try replace your code with this 你可以尝试用这个替换你的代码

<?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
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM