简体   繁体   English

CSS菜单上PHP的子菜单的活动/当前链接

[英]active/current links for sub menu in PHP on CSS menu

I have the below code for my CSS menu, it gets all the links from a database using PHP. 我的CSS菜单具有以下代码,它使用PHP从数据库中获取所有链接。

I have set active/current links for selected items which works fine for top level links but where i have sub menus it doesnt work. 我为所选项目设置了活动/当前链接,该链接对于顶级链接而言效果很好,但是在我有子菜单的地方,它不起作用。

for example, i have the following: 例如,我有以下内容:

Services
- Service 1
-- Sub Service 1

If Service 1 is selected, the Services link is active/current but if the Sub Service 1 link is selected, i want the Services link to be active/current but its not 如果选择了“ 服务 1” ,则“ 服务”链接处于活动状态/当前状态,但是如果选择了“ 子服务1”链接,则我希望“ 服务”链接处于活动状态/当前状态,但不是这样。

how can i fix this using the below code? 如何使用以下代码解决此问题?

<ul class="nav">
<?php
//select all the top row items
$sql="SELECT * from website_menu where parent_top = '' and parent = '' order by sequence ASC ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
  //then select all the next rows down (parent_top)
  $current = false;
  $subMenu = '';
  $sql2="SELECT * from website_menu where parent_top = '".$result["sequence"]."' order by sequence ASC ";
  $rs2=mysql_query($sql2,$conn) or die(mysql_error());
  if(mysql_num_rows($rs2) > 0)
  {
    $subMenu = '<ul>';
    while($result2=mysql_fetch_array($rs2))
    {
        if($_GET["id"] == $result2["link"])
        {
            $current = true;
        }
        $subMenu .= '<li><a href="'.$settings["website_url"].'/'.$result2["link"].'"><span>'.$result2["title"].'</span></a>';
        //
        $sql3="SELECT * from website_menu where parent = '".$result2["sequence"]."' ";
        $rs3=mysql_query($sql3,$conn) or die(mysql_error());
        if(mysql_num_rows($rs3) > 0)
        {
            $subMenu .='<ul>';
            while($result3=mysql_fetch_array($rs3))
            {
                $subMenu .='<li><a href="'.$settings["website_url"].'/'.$result3["link"].'"><span>'.$result3["title"].'</span></a></li>';
            }
            $subMenu .='</ul>';
            $subMenu .='</li>';
        }
        else
        {
            $subMenu .='</li>';
        }
    }
    $subMenu .= '</ul>';
}
    echo '<li';
if($_GET["id"] == $result["link"] || $current)
{
    echo ' class="active"';
}
echo '><a href="'.$settings["website_url"].'/'.$result["link"].'"><span>'.$result["title"].'</span></a>', $subMenu, '</li>';
 }
?>
</ul>

You might want to try this 您可能想尝试一下

In your following while loop make this change 在您的以下while loop进行此更改

while($result3=mysql_fetch_array($rs3))
    {
       $subMenu .='<li><a href="'.$settings["website_url"].'/'.$result3["link"].'"><span>'.$result3["title"].'</span></a></li>';
       // add this if block
       if($_GET["id"] == $result3["link"])
       {
          $current = true;
       }
    }

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

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