Problem using include php different folders

Asked

Viewed 2,608 times

4

First I apologize for not being able to express myself correctly, I am learning php yet and I came across the following problem. I’m setting up a site for studies and at the root I created the folders:

  • css (where is the materialize file I need to call)
  • template (where I created a file called cabecalho.php and in it I keep the call to the materialize css file and my navbar)
  • Tasks (where I created the file tarefa.php which by including call the cabecalho.php in the template folder and have the problem).

In the root folder I have the index.php

In the index.php I give the following include:

include ('template/cabecalho.php');

It then loads my header by calling the materialize css file and my navbar. In the.php header file I call the materialize css file as follows:

  <link type="text/css" rel="stylesheet" href="css/materialize.min.css"  media="screen,projection"/>

Even there everything perfect, my index loads normally.

My problem happens when I access the file tarefa.php, when I give include no cabecalho.php it does not load the materialize file getting totally disfigured my navbar and not loading the framework.

In the tarefa.php my include got like this:

include ('../template/cabecalho.php');

With this it loads the navbar no more the file . css of materialize.

Follow prints:

Pasta raiz no windows

projeto

  • 1

    This is because the files are at the same level, but in different directories, you just need to change 'css/materialize' to '.. /css/materialize'

  • Doing this right in the.php task file but in the index.php it is not configured since it calls the same.php header and it is not able to access the materialize file that is inside the css folder at the root. I’m looking for a solution that works for both

  • Well, I couldn’t quite understand how you make the inclusions, but from what I’ve seen this order has no sense or need to be made, why include a file that will include another ? from the moment a include is given, what is in the included file becomes part of the file that requested the inclusion, so if you include it in the index, you would have to point as if the.php header was part of the index.

  • You can print out your file folders?

  • And kind of confusing and probably not the right way to do it, plus what I’m doing and the following, the index.php file is at the root and the tasks.php file is inside the tasks folder. Both give include in the header.php that is inside the template folder. Here comes the problem why the.php header file calls the materialize.css file that is inside the css folder. Just as the index.php file and tasks.php are in different directories I end up having error in the call of the file materialize.css inside the file cabecalho.php

  • I added 2 prints to the question, I can’t add more because my reputation is low.

  • Try it this way: href="/css/materialize.min.css"

  • In the index, Voce includes.php tasks, in.php tasks.Voce includes.php header, and in the header you include css/materialize, that’s it ?

  • Both in index.php (which is at the root) and in the task.php (which is in the task folder) I give a include in the.php header that is in the template folder. There in the header.php I only have html call the file materialize.css that is inside the css folder. Looking through the structure print to understand. The problem is that when I open the.php task in the browser it cannot locate the path of materialize.css

  • Place a variable at the beginning of the pages (before include header) $page="index" in index.php and $page="task" in task.php. In the.php header make an if Else. If ($page=="task"){ .. /css/materialize }Else{ css/materialize

  • I avoid this problem by putting everything out of folders, I just leave inside the images folders.

Show 6 more comments

3 answers

5


Put a variable $pagina in the index.php and task pages.php

index php.

<?php
 $pagina="index";
 include ('template/cabecalho.php');

php task.

<?php
 $pagina="tarefa";
 include ('../template/cabecalho.php');

header.php

 if ($pagina=="tarefa"){

    echo '<link type="text/css" rel="stylesheet" href="../css/materialize.min.css"  media="screen,projection"/>';

 }

 if ($pagina=="index"){

    echo '<link type="text/css" rel="stylesheet" href="css/materialize.min.css"  media="screen,projection"/>';

 }

Or if applicable

 if ($pagina=="tarefa"){

    echo '<link type="text/css" rel="stylesheet" href="../css/materialize.min.css"  media="screen,projection"/>';

 }else{

    echo '<link type="text/css" rel="stylesheet" href="css/materialize.min.css"  media="screen,projection"/>';

 }

of course you can do it in other ways depending on your code:

if ($pagina=="tarefa"){ 
   $link="../css/materialize.min.css";
}else{ 
   $link="css/materialize.min.css";
}

  echo "<link type='text/css' rel='stylesheet' href='".$link."'  media='screen,projection'/>";

Or out of php

 <link type="text/css" rel="stylesheet" href="<?php echo $link ?>"  media="screen,projection"/>
  • Thank you very much, it was exactly what I wanted. In this case I adapted the 3° option that mounted in my project and worked perfect. Thanks again for the help.

3

Knowing your folder structure:

/
.../template/
         .../cabecario.php
.../tarefas/
        .../tarefas.php
.../css/
    .../materialize.min.css

index php.

include 'tarefas/tarefas.php'

php tasks.

include 'template/cabecario.php';

cabecario.php

 <link type="text/css" rel="stylesheet" href="css/materialize.min.css"  media="screen,projection"/>

All are part of index.php, then all the notes should come out as if they were index.php.

My index.php with this sequence:

<html>
    <head>
     <link type="text/css" rel="stylesheet" href="css/materialize.min.css" media="screen,projection">
    </head>
    <body>
    </body>
</html>
  • It is not the solution that I was looking for most helped me in other points, so I realized my problem is more in the organization. What I wanted is that the index is only called the.php header to load the materialize.css and the same thing with the.php tasks it should only load the materialize. Just that giving include in the.php tasks in index.php I will call the content of the.php tasks for index.php. I will try to organize myself better here and look for some organization tutorials, thanks for the help more already guided me well in my mistake.

  • Search by design Patterns, if you want to delve deeper into PHPOO and MVC structures, it may seem a bit difficult and confusing at first, but after you understand the concept your life will never be the same kk.

  • thanks, I’ll get started =)

1

I think you better always go to the server root, then from there you use the path.

/css/materialize.min.css">

This is a good practice in case you are going to call this file from several different folders, ai will never get lost because of the current folder ;).

Browser other questions tagged

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