How to build a includes system with $_GET

Asked

Viewed 55 times

0

I wanted to make my site lighter, it has 7 pages how to include all these pages in the home with the URL getting like this ?pagina=contato and so on to all pages

  • This seems to me to directly hurt the principle of the atomicity of the code and thus not be a good solution. What is the real need to put in a file only all the logic of the application?

  • make codes cleaner and I think lighter

2 answers

4

Make that friend

    <?php
$pagina = $_GET['pagina'];

   if($pagina == "contato"){
        include_once("Pages/contato.php");
   }elseif($pagina == "outra"){
       include_once("Pages/outra.php");
}
   ?>
  • Ai in the case in the menu the URL would look like this <a href="home.php?pagina=contato">Contato</a>?

  • Exactly, then when the page is equal to contacts it will call your page contacts

  • 1

    Tell me something else, does this system work with ID? example <a href="home.php?pagina=capítulos?id_livro=Genesis">Livros</a>

  • @mayron2017 makes a new If in id_book and in the next Array uses & instead of ?

  • <a href="home.php? pagina=capitulo&id_livro=Genesis>Livro</a>

  • Then from what I understood inside chapter will have something written that you want to use this tag Genesis just you receive the get variable from the id_book on the chapter page...

  • As well, not intense very well

  • Ex: <a href="home.php? pagina=capitulo&id_livro=Genesis>Book</a> the variable page vc paid on your homepage works the Else/If ... But the id_book , you get it on your chapter page.php

Show 4 more comments

1

this is how I make my PHP code:

<?php  
$url = isset($_GET['url']) ? $_GET['url'] : 'home';

if(file_exists('pages/'.$url.'.php')){
    include('pages/'.$url.'.php');
}else{
    include('pages/home.php');
}
?>

to use this method it is necessary to create a . htaccess file at the root of your site with this data:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Options -Indexes

You should Tamber create a constant in your config.php for your root directory, I put "INCLUDE_PATH", would look like this:

define("INCLUDE_PATH", "linkdoseusite.com/")

With this, just create the links in this way in html:

<a href="<?=INCLUDE_PATH?>meulink">carrinho</a>

This way when someone clicks on a link on your site, PHP will make a $_GET link and if there is a file with the same name of the link it will give a include of that file on your page. I hope you’ll be understanding!

Browser other questions tagged

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