Why doesn’t my switch generate the right results?

Asked

Viewed 56 times

1

I’m using a condition and a switch to determine some values for some variables, but I’m not getting the expected results.

When the link <a href="index.php?post=myFile"></a> is clicked, I hope to run the first case on the switch, but the desired variables are not overwritten.

This is my code that is in my file called index.php.

$titulo = '';
$keywords = '';
$descricao = '';
$post = empty($_GET['post']) ? '' : $_GET['post'];
$pagina = empty($_GET['p']) ? '' : $_GET['p'];

if ($post != '' || ($post == '' && $pagina != '')) {
    switch ($pagina):
    case 'myFile':
        $titulo = 'esta variável não muda o valor para esse arquivo';
        $keywords = 'esta variável não muda o valor para esse arquivo';
        $descricao = 'esta variável não muda o valor para esse arquivo';
        break;
    case 'home':
        $titulo = 'Página Inicial';
        break;
    case 'ultimasnoticias':
        $titulo = 'Ultimas Noticias';
        break;
    case 'contato':
        $titulo = 'Contato';
        break;
    case 'privacidade':
        $titulo = 'Privacidade';
        break;
    default:
        $titulo = 'Home';
        $pagina = 'home';
    endswitch;
} else {
    $titulo = 'Post';
}
<html>
<head>
<title><?php echo $titulo; ?></title>
<meta name="keywords" content="<?php echo $keywords; ?>">
<meta name="description" content="<?php echo $descricao; ?>">
</head>
<body>

    <?php
    if (empty($post)) {
    require_once 'page_' . $pagina . '.php';
    } else {
        require_once 'posts/' . $post . '.php';
    }
    ?>

<footer>Rodapé</footer>
</body>
</html>

My current results are:

$_GET["post"] = "myFile";
$_GET["p"] = NULL;
$titulo = "Home";
$keywords = "";
$descricao = "";
$pagina = "home";

My desired results are:

$_GET["post"] = "myFile";
$titulo = 'esta variável não muda o valor para esse arquivo';
$keywords = 'esta variável não muda o valor para esse arquivo';
$descricao = 'esta variável não muda o valor para esse arquivo';
$pagina = "home";

Why can’t I update the value of variables using the first instruction of case? In short, the values of mine case do not change only to files that are in a subdirectory

This is the code I use for my other links, which are in my main directory:

<nav>
   <ul>
     <li><a href="?p=home">Início</a></li>
     <li><a href="?p=ultimasnoticias">Últimas Notícias</a>
     <li><a href="?p=contato">Contato</a></li>
  </ul>
</nav>

Note: I’m not sure if the image below will help in something, but through the doubts I put:

inserir a descrição da imagem aqui

  • It is working yes: https://ideone.com/6WiYsS

  • @Maniero I’m doing something wrong and I couldn’t figure out what it is. Look, when I change the link from <a href="index.php?post=myFile"></a> for ?p=myFile yes the case changes the value of the variables. But it does not open the file inside the body tag, being that it is in a sub-directory.

  • So the question is about something else that is not described in the question? And we have no way of knowing what happens with the information posted?

  • @Maniero No, that’s exactly what I said in the question, the values of the case are only changed to the main directory files.

  • And I just showed that in the way you presented it is not true. It is only if there is something missing or misspelled.

  • @Maniero I tried to run my script on the ideone site as you had done. But a lot of error appeared. https://ideone.com/Mk9MnO If the case is really working, so what I’m doing wrong, or how I find out?

  • @Maniero I saw that on your link to the ideone you put these two lines $_GET["post"] = "myFile"; and $_GET["p"] = "myFile";, I put these lines in my script, and actually case is working. Then maybe the problem is the querystring I’m using? index.php?post=myFile

  • That’s what I said, there’s no way to know because the question doesn’t have enough information.

Show 4 more comments
No answers

Browser other questions tagged

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