Else in PHP to call menu according to URL

0

I do not handle a lot of PHP. I have a part of the site that contains a <a> with the current path where the client is.

This is the code:

<a href="
<?php if(preg_match("/segmentos/i", $_SERVER['REQUEST_URI']))echo '/segmentos/neomot-'.$url[0]; ?>
<?php if(preg_match('/elevadores\/atuacao/i',$_SERVER['REQUEST_URI'])) echo '/atuacao'; else echo '/produtos';?>">
<?php if(preg_match('/elevadores\/atuacao/i',$_SERVER['REQUEST_URI'])) echo 'Atua&ccedil;&atilde;o'; else echo 'Produtos';?>
</a>
What happens, it will check if the link is in /elevadores/iluminacao it will give a echo with the correct url, if it is not that and it is iluminacao/produtos it will do echo to produtos for example, and if I want to put one more condition? Type include cases also in the same way you are in products.

    
asked by anonymous 11.03.2015 / 19:21

1 answer

1

If you want to use case it is best to write a variable to the end of the URL and test it with switch. To do this, use the parse_url function:

<?php 
$uri = $_SERVER['REQUEST_URI']; // http://seusite.com/path
$parse_url = parse_url($url); // pega as infos do url
$path = $parse_url['path']; // pega a path 
$host = $parse_url['path']; // seusite.com só pra facilitar na hora do href
$scheme = $parse_url['scheme']; // http ou https?
$href = ''; //variável que vai conter o href dos links
$anchor = '';//variável que vai conter o anchor text dos links
switch ($path) {
    case '/segmentos/i':
        $href = $scheme . '://' . $host . '/segmentos/neomot-' . $url[0];
        $anchor = 'Produtos';
        break;
    case '/elevadores/atuacao/i':
        $href = $scheme . '://' . $host . '/atuacao';
        $anchor = 'Atua&ccedil;&atilde;o';
        break;
    default:
        $href = $scheme . '://' . $host . '/produtos';
        $anchor = 'Produtos';
        break;
}?>
<a href="<?php echo $href;?>"><?php echo $anchor;?></a>
    
11.03.2015 / 19:49