简体   繁体   English

如何在该特定页面上选择导航菜单?

[英]How to make navigation menu selected on that particular page?

I am building a navigation system on a webpage using PHP, that basically queries data from MySQL database and display as ul, li menu on main menu. 我正在使用PHP在网页上构建导航系统,该系统基本上从MySQL数据库查询数据并在主菜单上显示为ul,li菜单。 But I couldn't figure out how do I make navigation menu selected when user goes to that page. 但是我不知道当用户转到该页面时如何选择导航菜单。 I have one idea but that doesn't make home menu selected by default when user enters that page. 我有一个主意,但是当用户进入该页面时,默认情况下不会使主菜单处于选中状态。

I have also a CSS class written to give that class to the anchor tag if it is that page. 我还编写了一个CSS类,以将该类提供给锚标记(如果它是该页面)。

here is my PHP code: 这是我的PHP代码:

//Query Functions 
function queryMenu() {
    $query = "SELECT * FROM menu_en";
    return $query;  
}

// Render Functions
function renderNav() {  
    $menus = queryMenu();
    $queryMenu = mysql_query($menus);
    while( $navigation = mysql_fetch_array($queryMenu) ){
        if( $navigation['id'] == $_GET['pageId'] ){
            echo '<li>
                    <a href="index.php?pageId="'.$navigation['id'].' class="selected" >'.$navigation['menu_title'].
                 '</a></li>';
        }else{
            echo '<li><a href="index.php?pageId="'.$navigation['id'].'>'.$navigation['menu_title'].'</a></li>';
        }
    }
}

Suggest me a better way to do this, as I want to enhance my skill in PHP. 向我建议一个更好的方法,因为我想提高自己的PHP技能。 And also please let me know if there is any wrong approach in above code. 同时请让我知道以上代码中是否有错误的方法。

If the home page is when $_GET['pageId'] is not set you could check using isset() 如果主页未设置$ _GET ['pageId'],则可以使用isset()检查

if(isset($_GET['pageId'])) {
    $page = $_GET['pageId']
} else {
    $page = $homePageId; // the ID of the home page. 
}

Then you would replace this line: 然后,您将替换以下行:

if( $navigation['id'] == $_GET['pageId'] ){

with this line: 用这一行:

if( $navigation['id'] == $page ){

I think this would work, if i understand you correctly. 如果我正确理解你的话,我认为这会起作用。

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

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