Problem in using include php different folders

3

First of all I apologize for not being able to express myself correctly, I am still learning php and I came across the following problem. I am setting up a site for studies and in the root I created the folders:

  • css (where is the materialize file that I need to call)
  • template (where I created a file named cabecalho.php and saved in it the call to the materialize css file and my navbar)
  • Tasks (where I created the file tarefa.php that by include I call the cabecalho.php in the template folder and I have the problem).

In the root folder I have the index.php

In 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 file header.php I call the css file of materialize as follows:

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

So long everything perfect, my index loads normally.

My problem happens when I access the tarefa.php file, when I give include in cabecalho.php it does not load the materialize file, leaving my navbar totally unconfigured not loading the framework.

In tarefa.php my include looks like this:

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

With this it loads the navbar but not the materialize .css file.

Follow prints:

    
asked by anonymous 17.08.2017 / 20:42

3 answers

3

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

index.php

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

task.php

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

cabecalho.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"/>
    
17.08.2017 / 22:21
2

Knowing your folder structure:

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

index.php

include 'tarefas/tarefas.php'
  

tareas.php

include 'template/cabecario.php';
  

cabecario.php

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

Everyone is part of index.php , so all notes should come out as if they were index.php .

My index.php with this string:

<html>
    <head>
     <link type="text/css" rel="stylesheet" href="css/materialize.min.css" media="screen,projection">
    </head>
    <body>
    </body>
</html>
    
17.08.2017 / 21:29
0

I think you should always go to the root of the server, then you use the path from it.

/css/materialize.min.css ">

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

    
18.08.2017 / 01:01