Search code examples
phpclassdynamic-data

Add active class to the selected link in php generated menu


I'm want to add class="active" in list item to highlight the selected menu but I couldn't figure it out how to do this since I'm not really familiar with php.

this is what I have in tpl file

<ul class="menu">
<li>
    <a<?php echo $target; ?> href="<?php echo $link; ?>"<?php echo $nofollow; ?>><?php echo $this->escape_html($link_title); ?></a>
    <?php echo $sub_menu; ?>
</li>

and in php file

        $links = $this->db->GetAll($query);
    foreach($links as $link) {
        $template = $this->PMDR->getNew('Template');
        $template->set('link',$link['url']);
        $template->set('link_title',$link['title']);
        $template->set('nofollow',($link['nofollow'] ? ' rel="'.$link['nofollow'].'"' : ''));
        $template->set('target',($link['target'] ? ' target="'.$link['target'].'"' : ''));
        $template->set('indent',str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$level));
        ob_start();
        $this->getMenuLoop($link['id'], $level+1);
        $sub_menu = ob_get_clean();
        if(!empty($sub_menu)) {
            $menu_template = $this->PMDR->getNew('Template',$this->template);
            $menu_template->set('items', $sub_menu);
            $sub_menu = $menu_template->render();
        }
        $template->set('sub_menu', $sub_menu);
        echo $template->render($this->item_template);
    }
}

Thank you in advance!


Solution

  • In your tpl, add $class (I put it with $target, but it can be anywhere inside the <a>)

    <ul class="menu">
    <li>
        <a<?php echo $target; echo $class; ?> href="<?php echo $link; ?>"<?php echo $nofollow; ?>><?php echo   $this->escape_html($link_title); ?></a>
        <?php echo $sub_menu; ?>
    </li>
    

    The in your php file add

    $current_url = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    

    AND

    $template->set('class',(($current_url == $link['url']) ? ' class="active"' : ''));
    

    this check to see if the current url is the same as the $link['url'], and adds `class="active"

        //gets the current page
        $current_url = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    
        $links = $this->db->GetAll($query);
    foreach($links as $link) {
        $template = $this->PMDR->getNew('Template');
        $template->set('link',$link['url']);
        $template->set('link_title',$link['title']);
        $template->set('nofollow',($link['nofollow'] ? ' rel="'.$link['nofollow'].'"' : ''));
        $template->set('target',($link['target'] ? ' target="'.$link['target'].'"' : ''));
        $template->set('class',(($current_url == $link['url']) ? ' class="active"' : ''));
        $template->set('indent',str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$level));
        ob_start();
        $this->getMenuLoop($link['id'], $level+1);
        $sub_menu = ob_get_clean();
        if(!empty($sub_menu)) {
            $menu_template = $this->PMDR->getNew('Template',$this->template);
            $menu_template->set('items', $sub_menu);
            $sub_menu = $menu_template->render();
        }
        $template->set('sub_menu', $sub_menu);
        echo $template->render($this->item_template);
    }
    

    }