Menu with active class in php?

Asked

Viewed 82 times

0

I have a menu in html :

        <ul>
        <li><a href="#" class="ativa">Página Inicial</a></li>
            <li><a href="servicos">Serviços</a></li>                  
            <li><a href="entretenimento">Entretenimento</a></li>
            <li><a href="festas-e-decoracoes">Festas e Decorações</a></li>
            <li><a href="bares-e-restaurantes">Bares e Restaurantes</a></li>    
            <li><a href="beleza-e-estetica">Beleza e Estética</a></li>
            <li><a href="hoteis-e-pousadas">Hotéis e Pousadas</a></li>
            <li><a href="esportes-e-suplementos">Esportes e Suplementos</a></li>
            </ul>   

But I want it to be in php due to the database I created for url friendly, for example: /categories/services

How do I continue my menu with active class, but being done in php?

The code is for a getMenu but I don’t know if it’s correct.

public function getMenu(){
        $pegar_categorias = "SELECT * FROM 'categorias' ORDER BY id DESC";
        $executar = self::conn()->prepare($pegar_categorias);
        $executar->execute();
        if($executar->rowCount()==0){}else{
            while($categoria = $executar->fetchObject()){
                echo '<li><a href="'.Path.'/categoria/'.$categoria->slug.'">'.$categoria->titulo.'';
                echo '</li>';
            }
        }

    }

Thank you!

  • Where is the php code? At least php should have information about which page is the current and this information should be related to the menu.

  • I edited and added php code!

  • 1

    Makes a echo $_SERVER['REQUEST_URI']; exit; right at the top of the page and you might get a sense of where to start.

  • Following what @Danielomine said, compare the $categoria->slug with the $_SERVER['REQUEST_URI'] to put an "active" class in the read that is related to the page that is open

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.