Dynamic links with parameters

Asked

Viewed 96 times

0

Hello!

I have a question. I would like to create a file external to the menu of a site that is responsible for making the link between the pages, but I’m having a lot of difficulty executing. I’m starting with PHP now...

For example:

My menu has:

<a class="nav-link" href="./r.php?codigo=0" id="hl">Home</a>
<a class="nav-link" href="./r.php?codigo=1" id="hl">Quem Somos</a>
<a class="nav-link" href="./r.php?codigo=2" id="hl">Textos</a>
<a class="nav-link" href="./r.php?codigo=3" id="hl">Contato</a>


In the r.php file I am using the superglobal $_GET to get this information:

<?php
 $link = $_GET['codigo'];
 $endereco = array("./index.php","./quemsomos/index.php","./textos/index.php","./contato/index.php");

 header('location: ' . $endereco[$link]);
?>

The first one works, because the r.php file is in the root folder, next to the first index.php. The others can’t find the file and I’m having a hard time doing it.

Had read that './' does the search from the root folder of the application, but is not working. Any hint?

Thank you in advance.

1 answer

1


Instead of creating index.php within multiple folders you can do it as follows: Leave a file index.php at the root and inside that file put your menu.

Create a folder named after the root "pages". Within this folder create the files: home.php , quemsomos.php , textos.php , contato.php

Then, still at the root, create a file called paginacao.php Inside this file write the following code:

<?php

switch ($_GET['codigo']) {

    case '0':
        $conteudo = "home.php";
    break;

    case '1':
        $conteudo = "paginas/quemsomos.php";
    break;

    case '2':
        $conteudo = "paginas/textos.php";
    break;

    case '3':
        $conteudo = "paginas/contato.php";
    break;

    default:
        $conteudo = "home.php";
    break;
}

?>

Back on page index.php from the root and put the following code after your menu:

<?php 

    include("paginacao.php");
    include($conteudo);
?>
  • I’ve been researching and doing something similar to this. And another, I’ve added this same logic to a page that dynamically shows the texts/posts. Thanks for the help!

Browser other questions tagged

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