Dynamic links with parameters

0

Hello

I have a question. I would like to create a file outside the menu of a website that is responsible for linking the pages, but I am having a lot of difficulty in running it. 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'm 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 works because the r.php file is in the root folder, next to the first index.php. Others can not find the file and I'm having trouble doing so.

I had read that the './' searches from the root folder of the application, but it is not working. Any tips?

Thank you in advance.

    
asked by anonymous 08.12.2018 / 11:54

1 answer

1

Instead of creating index.php within several folders you can do it like this: Leave a file index.php at root and within that file place your menu .

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

Then, still in the root, create a file called paginacao.php Within this file type 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;
}

?>

Return to the page index.php of the root and put the following code after your menu :

<?php 

    include("paginacao.php");
    include($conteudo);
?>
    
10.12.2018 / 05:53