How to open an .php file that is in a subdirectory?

6

My index.php has this script inside it:

<?php

$pagina = empty($_GET['p']) ? 'home' : $_GET['p'];

switch ($pagina):
case 'contato':
    $titulo = 'Contato ';
    $keywords = '';
    $descricao = '';
    break;

case 'privacidade':
    $titulo = 'Privacidade ';
    $keywords = '';
    $descricao = '';
    break;

case 'ultimasnoticias':
    $titulo = 'Ultimas Noticias';
    $keywords = '';
    $descricao = '';
    break;

default:
    $titulo = 'Home';
    $keywords = '';
    $descricao = '';
    $pagina = 'home';
endswitch;
?>
<html>
<head>
<title><?php echo $titulo; ?></title>
<meta name="keywords" content="<?php echo $keywords; ?>">
<meta name="description" content="<?php echo $descricao; ?>">
</head>
<body>

    <?php require_once 'page_' . $pagina . '.php'; ?>

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

I'm having a lot of trouble explaining my problem, so I tried to explain using the image below.

    
asked by anonymous 29.08.2018 / 18:31

3 answers

4

You should figure out how to pass the post name to your php script. It could even use the same attribute p and give a change in the current code, or use another attribute and pass the name of the post. I will show using the 2nd option.

First, define the new variable, I will use here in the example the variable post via GET . I also defined the other variables here at the beginning of the script

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

In case, the decision code on the page that already exists should only run if the variable is not informed, or it is blank. In this case, you can put all switch within if . Put a else if you want to populate the other variables.

if (empty($post)) {
    // switch aqui
} else {
    $titulo = 'Post';
}

And finally, at the time of require_once , again see if $post is empty, if it is do what it does today, otherwise call the file in /post .

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

To create the link within the pages, let's say for the meuArquivo.php post as you put it in the image, it would look like this:

<a href="index.php?post=meuArquivo">Meu Arquivo</a>

Now, the script together would look like this:

<?php

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

$pagina = empty($_GET['p']) ? 'home' : $_GET['p'];

if (empty($post)) {
    switch ($pagina):
    case 'contato':
        $titulo = 'Contato ';
        break;
    case 'privacidade':
        $titulo = 'Privacidade ';
        break;
    case 'ultimasnoticias':
        $titulo = 'Ultimas Noticias';
        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>
    
29.08.2018 / 19:52
2

To access files from another folder has no secret:

You are in: index.php

<a href="posts/meuArquivo.php">Página 1 </a>

Now you are in: posts / myfile.php and want to go back to index or another index brother file:

<a href="/">Página 1 </a>

<a href="/page_home.php">Página 1 </a>
    
29.08.2018 / 18:36
1

I do not know if I understood very well, but let's go.

  • To open the [posts] directory link you use

    < a href="/post/nome_arquivo.php"> arquivo da pasta post < /a >
    
  • If you do not want to point the directory, you can pass a parameter via $ _GET and in your switch you treat which page gives required_once . my advice is you do not do this 'page_'. $ page. '.php' And in your case put something like case 'ultimasnoticias': $pagina = "caminho/ultimasnoticias.php" . Telling the full path to be directed.

  • Credit that I answered in the first item.

    < a href="/ post / filename.php" > file from the post

  • 29.08.2018 / 19:25