Else in PHP to call menu as URL

Asked

Viewed 94 times

0

I don’t know much about PHP. I have a part of the site that contains a <a> with the current path from where the customer is.

That 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 on /elevadores/iluminacao he’s gonna give a echo with the correct url, if not this and is iluminacao/produtos he will echo to produtos for example, what if I want to put one more condition? Type include cases also, in the same way that is in products.

  • No longer has an example of else right there on the last line?

  • Just include ; else echo 'Cases'?

  • Your question is a little confused. In which case you want to print cases?

  • if the link is /cases, the same is the call in /produtos.

1 answer

1


If you want to use case the best is to write in a variable the final part of the URL and test it with switch. This is done using the function parse_url:

<?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>

Browser other questions tagged

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