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

Asked

Viewed 1,597 times

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 hard time explaining my problem, so I tried to explain using the image below.

inserir a descrição da imagem aqui

  • I don’t know if I understand, but putting require_once 'posts/page_' .... is not that?

  • if that’s what you want to know, to access files inside directories, use /, for example, posts/meuprimeiropost.php

  • I have to say, you’ve managed to summarize the problem in this picture much better than the other four or five times together. Now it will be possible to draw up a reply.

  • Whew, at least now came a post that actually has something that opens file! The previous ones did not speak anything of require. See if next time you ask the question with all the details instead of creating 3289 accounts (and think it’s stalking). With details it is possible to help you. Without details, there is no willingness to guess what you want.

  • @Bacco I’m sorry, I’m new to PHP, so I didn’t know which codes were relevant to show, but I realized that in doubt it’s better to show everything related (no exaggeration).

  • Yes, and stop thinking we’re chasing you. Simply read what is asked in the comments and try to help us solve your problem. Creating accounts and accounts and reposting the same thing disturbs the whole site. Just edit the question as comments ask for your help, and create new question only after solving the previous step.

Show 1 more comment

3 answers

4


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

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

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

In this case, the decision code on the page that already exists should only rotate if the variable is not informed, or comes blank. In that case, you can put the whole switch within a if. Put a else if you want to fill in the other variables.

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

And finally, at the hour of require_once, see again if the $post is empty, if you are doing what you already do 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, say to the post meuArquivo.php as you put it in the picture, it would be like this:

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

Now, the script with everything 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>
  • @It worked perfectly, only one thing you explained and I think I didn’t understand right, so in the files of my main directory this showing this error message just above the header Notice: Undefined index: post in C:\xampp\htdocs\site\index.php on line 6 this is line 6 $post = $_GET['post'];.

  • Try trading for $post = empty($_GET['post']) ? '' : $_GET['post']; and see if the message disappears. If it disappears I update the reply

  • The message is gone, it’s perfect. Thank you very much for the help! Note: There is a ")" in these lines if (empty($post)

  • I noticed that I’m not able to change the value of $titulo $descricao and $keywords of the files that are inside the "posts" folder. I should inform the values of these variables in the case right? I can only change the values in the variables that are at the top after <?php, but I need to put a $titulo...etc, single for each page that is inside the folder "posts".

  • I tidied it up there... About the valuables, I won’t be able to test it here, but you tried to print them after the require_once? I’m almost sure they change, because they are global. The problem is that you print them before you change right? The same should happen in descricao and keywords with the other pages, if you repair. getTitulo() and getConteudo(), do the require_once before writing the page and replacing these variables with these functions... is worth a test

  • I believe this may be a new question, maybe it’s better to create a new one...

  • All right colleague, thank you, your help played a key role in my project.

Show 2 more comments

2

To access files from another folder has no secret:

You’re in: index php.

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

Now you’re in: posts/myFile.php and wants to return to index or other index sister file:

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

<a href="/page_home.php">Página 1 </a>
  • @Andersoncarloswoss Exactly, how do I do that? I tried to put one require under the other require that is in index.php but it didn’t work. I suppose I need only 1 require no index,php along with a variable that is configured to do what I explained in the image.

  • You can do an if before and call require with the correct variable.

1

I don’t know if I understand much more we go there.

  1. To open the link of the directory [posts] you use

    < a href="/post/nome_arquivo.php"> arquivo da pasta post < /a >
    

If you do not want to point to the directory, you can pass a parameter via $_GET and in your switch you treat which page to give a required_once. my advice is that you don’t do this 'page_' . $page . '. php' And in your case put something like case 'ultimasnoticias': $pagina = "caminho/ultimasnoticias.php". Informing the complete path to be directed.

  1. credit I answered in the first item.

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

Browser other questions tagged

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