How to add a class to a li when the person is on the link page accessed?

2

I'm creating a site using PHP with includes for better maintenance, however I'm having a problem.

If I used only HTML this problem would not be occurring, as I'm using includes to pull menu , I can not add a class to all pages.

I have a menu that uses code like this:

<ul id="menu" class="clearfix">
     <li>
         <a href="index.php">Início</a>
     </li>
     <li>
         <a href="sobre.php">Sobre</a>
     </li>
     <li>
         <a href="contato.php">Contato</a>
     </li>
</ul>

Now let's suppose that I'm on the page index.php so you have to add a class to <li> Home .

For a better understanding, see:

As it is if I'm on the index.php page ):

HowdoIgetifI'montheindex.phppage(right):

Example:

  

If the person is on the page index.php has to be added to the active class in <li> Home .

     

If the person is on the page sobre.php it has to be added to the active class in <li> About and so on.

In case you have to add a class, so, eg:

<li class="ativo">
         <a href="index.php">Início</a>
</li>

Note: This reminded me a lot of permalink function of Wordpress.

  

An example is the WP Total site: link

When I enter the site, the name Início in the menu is orange.

IfIaccessthepageabout,theAboutnameinthemenubecomesorange.

    
asked by anonymous 23.07.2014 / 01:16

2 answers

2

No menu.php is just to do:

<ul id="menu" class="clearfix">
     <li <?php if($verifica['index']==true) echo "class='active'" ?> >
         <a href="index.php">Início</a>
     </li>
     <li <?php if($verifica['sobre']==true) echo "class='active'" ?> >
         <a href="sobre.php">Sobre</a>
     </li>
     <li <?php if($verifica['contacto']==true) echo "class='active'" ?> >
         <a href="contato.php">Contato</a>
     </li>
</ul>

And before% w / o% w / o% w / w you have% w / w% w / w /

Example in include :

$verifica['index']=false;
$verifica['sobre']=true;
$verifica['cintacto']=false;
include 'menu.php';

NOTE:

But I would do otherwise with menu.php . You put everything in the menus, header , footer , etc in true and then included the other pages in that false through sobre.php .

Then just make $_GET in index.php and make index.php :

index.php:

<ul id="menu" class="clearfix">
     <li <?php if($_GET['pagina']=="inicio") echo "class='active'" ?> >
         <a href="index.php">Início</a>
     </li>
     <li <?php if($_GET['pagina']=="sobre") echo "class='active'" ?> >
         <a href="sobre.php">Sobre</a>
     </li>
     <li <?php if($_GET['pagina']=="contato") echo "class='active'" ?> >
         <a href="contato.php">Contato</a>
     </li>
</ul>

<?php
    switch($_GET['pagina']){
         case "inicio":
              //não faz nada
              break;

         case "sobre":
              include 'sobre.php';
              break;

         case "contato":
              include 'contato.php';
              break;
    }
?>
    
23.07.2014 / 01:50
1

I think something like this is more practical:

<?php
$pagina_atual = basename($_SERVER['PHP_SELF']);
?>
<ul id="menu" class="clearfix">
     <li<?php echo ($pagina_atual == 'index.php' ? ' class="ativo"' : ''); ?>>
         <a href="index.php">Início</a>
     </li>
     <li<?php echo ($pagina_atual == 'sobre.php' ? ' class="ativo"' : ''); ?>>
         <a href="sobre.php">Sobre</a>
     </li>
     <li<?php echo ($pagina_atual == 'contato.php' ? ' class="ativo"' : ''); ?>>
         <a href="contato.php">Contato</a>
     </li>
</ul>
    
28.07.2014 / 01:12