menu marked according to php page

Asked

Viewed 55 times

1

someone has some idea of how I can leave the menu marked according to the user’s page, I made a way here, however and much check, taking into account that I have several pages, vo posted as I did

if(isset($urlE[1]) && $urlE[1] == 'a'){
    echo '
        <li><a href="'.$urlBase.'" style="width:45px;border-radius:2px;">HOME</a></li>
        <li><a href="javascript:void(0)" class="ativo">A</a></li>
        <li><a href="'.$urlBase.'/filtragem/b">B</a></li>
        <li><a href="'.$urlBase.'/c">C</a></li>
        <li><a href="'.$urlBase.'/d">D</a></li>
        <li><a href="'.$urlBase.'/e">E</a></li>
        <li><a href="'.$urlBase.'/f">F</a></li>
        <li><a href="'.$urlBase.'/g">G</a></li>
        <li><a href="'.$urlBase.'/h">H</a></li>
        <li><a href="'.$urlBase.'/i">I</a></li>
        <li><a href="'.$urlBase.'/j">J</a></li>
        <li><a href="'.$urlBase.'/k">K</a></li>
        <li><a href="'.$urlBase.'/l">L</a></li>
        <li><a href="'.$urlBase.'/m">M</a></li>
        <li><a href="'.$urlBase.'/n">N</a></li>
        <li><a href="'.$urlBase.'/o">O</a></li>
        <li><a href="'.$urlBase.'/p">P</a></li>
        <li><a href="'.$urlBase.'/q">Q</a></li>
        <li><a href="'.$urlBase.'/r">R</a></li>
        <li><a href="'.$urlBase.'/s">S</a></li>
        <li><a href="'.$urlBase.'/t">T</a></li>
        <li><a href="'.$urlBase.'/u">U</a></li>
        <li><a href="'.$urlBase.'/v">V</a></li>
        <li><a href="'.$urlBase.'/w">W</a></li>
        <li><a href="'.$urlBase.'/x">X</a></li>
        <li><a href="'.$urlBase.'/y">Y</a></li>
        <li><a href="'.$urlBase.'/z">Z</a></li>
    ';
}elseif(isset($urlE[1]) && $urlE[1] == 'b'){
    echo '
        <li><a href="'.$urlBase.'" style="width:45px;border-radius:2px;">HOME</a></li>
        <li><a href="'.$urlBase.'/filtragem/a">A</a></li>
        <li><a href="javascript:void(0)" class="ativo">B</a></li>
        <li><a href="'.$urlBase.'/filtragem/c">C</a></li>
        <li><a href="'.$urlBase.'/filtragem/d">D</a></li>
        <li><a href="'.$urlBase.'/filtragem/e">E</a></li>
        <li><a href="'.$urlBase.'/filtragem/f">F</a></li>
        <li><a href="'.$urlBase.'/filtragem/g">G</a></li>
        <li><a href="'.$urlBase.'/filtragem/h">H</a></li>
        <li><a href="'.$urlBase.'/filtragem/i">I</a></li>
        <li><a href="'.$urlBase.'/filtragem/j">J</a></li>
        <li><a href="'.$urlBase.'/filtragem/k">K</a></li>
        <li><a href="'.$urlBase.'/filtragem/l">L</a></li>
        <li><a href="'.$urlBase.'/filtragem/m">M</a></li>
        <li><a href="'.$urlBase.'/filtragem/n">N</a></li>
        <li><a href="'.$urlBase.'/filtragem/o">O</a></li>
        <li><a href="'.$urlBase.'/filtragem/p">P</a></li>
        <li><a href="'.$urlBase.'/filtragem/q">Q</a></li>
        <li><a href="'.$urlBase.'/filtragem/r">R</a></li>
        <li><a href="'.$urlBase.'/filtragem/s">S</a></li>
        <li><a href="'.$urlBase.'/filtragem/t">T</a></li>
        <li><a href="'.$urlBase.'/filtragem/u">U</a></li>
        <li><a href="'.$urlBase.'/filtragem/v">V</a></li>
        <li><a href="'.$urlBase.'/filtragem/w">W</a></li>
        <li><a href="'.$urlBase.'/filtragem/x">X</a></li>
        <li><a href="'.$urlBase.'/filtragem/y">Y</a></li>
        <li><a href="'.$urlBase.'/filtragem/z">Z</a></li>
    ';
}

and so on, is there any way to make it cleaner? it has to be in php, because the refresh page

1 answer

3


Basically this (adjust the variables):

for ($i=0; $i < 26; $i++) {
   $letra=Chr(97+$i);

   if ($urlAtual == $letra) {
      echo "<html do $letra SELECIONADO>";
   } else {
      echo "<html do $letra normal>";
   }
}       

See working on IDEONE.

The function Chr generates a character according to the ASCII table (97 is the a);


If using a list of words instead of letters:

$palavras = ['suspense','acao','terror','educativo-xxx','comedia','documentario'];

foreach ($palavras as $palavra) {
   if ($urlAtual == $palavra) {
      echo "<html do $palavra SELECIONADO>".PHP_EOL;
   } else {
      echo "<html do $palavra>".PHP_EOL;
   }
}  

See working on IDEONE.

  • 1

    yeah, I got it $urlAtual = $urlE[1];&#xA; for ($i=0; $i < 26; $i++){&#xA; $letra = Chr(97+$i);&#xA; if($urlAtual = $letra){&#xA; if(isset($urlE[1]) && $urlE[1] == $letra){&#xA; echo "<li><a href='".$urlBase."/filtragem/".$letra."' class='ativo' style='text-transform:capitalize;'>".$urlAtual."</a></li>".PHP_EOL;&#xA; }else{ &#xA; echo "<li><a href='".$urlBase."/filtragem/".$letra."' style='text-transform:capitalize;'>".$urlAtual."</a></li>".PHP_EOL;&#xA; }&#xA; }&#xA; }, you and beast, likezao

  • 1

    Try to understand how it works, it’s important for you to improve your code in other parts in the future. Don’t just adapt, try to understand.

  • I have one more menu, so I understood this line $letra=Chr(97+$i); generates the alphabet, as I would mark the menu that its url and so http://localhost/local/genero/acao, how I would change the value acao

  • 1

    You can use an array of words and a foreach https://ideone.com/3LKMo9 - updated the response

Browser other questions tagged

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