简体   繁体   中英

PHP give active class to active item in navigation

I'm trying to give the active nav item a .active class using php but i don't know any solution to make this happen. At the moment its always number 1 active even if its not. I don't really see the problem so i was hoping you guys might can help me out. This is what i have at the moment:

<?php
$pages = $db->queryarray("
        SELECT p.name, 
               u.url 
        FROM pages p
            , url u 
        WHERE p.active=1 
            AND p.hidden=0 
            AND p.root=0 
            AND p.menu=1 
            AND u.type='s' 
            AND u.source=p.id 
        ORDER BY p.position ASC
    ");

$i=1;

foreach($pages as $page)
{
    echo '<li>
                <a title="Pagename"'.($i==1?' class="active"':"").'href="index.php#page_'.str_replace(' ','_',strtolower($page['name'])).'">'.$page['name'].'</a>
          </li>';
    $i++;
}
?>

@mhakvoort

echo '<li><a title="Pagename" class="'.($page['active']) ? "active" : "" .'" href="index.php#page_'.str_replace(' ','_',strtolower($page['name'])).'">'.$page['name'].'</a></li>';

The first will always be true because of your if statement $i == 1;

To add the class active to your active page you would need something like this, depending on your field names.:

In your query you need to add the active boolean, and check on it in the a tag:

<?php
$pages = $db->queryarray("
        SELECT p.name,
               p.active, 
               u.url 
        FROM pages p
            , url u 
        WHERE p.active=1 
            AND p.hidden=0 
            AND p.root=0 
            AND p.menu=1 
            AND u.type='s' 
            AND u.source=p.id 
        ORDER BY p.position ASC
    ");

$i=1;

foreach($pages as $page)
{
    echo '<li>
                <a title="Pagename"'.($page['active']) ? 'class="active"' : "").'href="index.php#page_'.str_replace(' ','_',strtolower($page['name'])).'">'.$page['name'].'</a>
          </li>';
    $i++;
}
?>

You need to have an identifier which page is the current page therefore we can take advantage of $_GET php variable.

Please note i have change index.php#page_ to index.php?page=

foreach ($pages as $page) {
         $pageIdentifier = str_replace(' ','_',strtolower($page['name']));

         $findActivePage = isset($_GET['page']) && $_GET['page'] ==  $pageIdentifier? " class='active' ": "";

         echo '<li><a title="Pagename" '.$findActivePage.' href="index.php?page='.$pageIdentifier.'">'.$page['name'].'</a></li>';

    }




You can also use jquery for adding active class when your anchor tag is click.Note I have added a menu_item class to your anchor tag.

foreach ($pages as $page) {
         $pageIdentifier = str_replace(' ','_',strtolower($page['name']));
         echo '<li><a  class="menu_item" id="page_'.$pageIdentifier.'" title="Pagename" href="index.php#page_'.$pageIdentifier.'">'.$page['name'].'</a></li>';

    }

Jquery code

 //menu item click event.

    $("a.menu_item").on("click",function(){
        //Remove active class in all a.menu_item
        $("a.menu_item").removeClass("active");

        //Add active class to the clicked menu_item
        $(this).addClass("active");
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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