Search code examples
phphtmlhttp-status-code-404require

How include footer and header in my views?


First of all, sorry for english grammar, not my native language.

Before I start I want to clarify that I am implementing all this in a boostrap 4 template called Admin SB 2.

In this small project, inside my main folder, I have my index.php file, inside my project folder I have a directory called views where I got footer.php and header.php and inside index.php I include footer.php and header.php, for example:

<?php require_once("views/header.php")?>

<!--START OF MAIN CONTENT -->

<div class = "container">
    <h1> Main content </h1>

</div>
<!-- END OF MAIN CONTENT -->

<?php require_once("views/footer.php")?>

Everything excellent up to here, the problem begins when inside my views directory, I have another directory called patients which is where I plan to save everything related to the patients module. Here I have this"

// header
<? php require_once ("../ header.php")?>
// here goes my content
// footer
<? php require_once ("../ footer.php"); ?>

So, the problems that the console gives me are these:

Problem

I can understand that they are errors of the direction of the necessary files for the design and operation of the page, but if I edit the address in the header.php and footer.php files, I can solve the problem of the files within directories in the views , but the index.php will stop working.

What can you recommend me to do?

Have a happy rest of the day


Solution

  • To include the header and footer file in a page, use the include statement:

    <!--header start -->
        <?php include("includes/header.php"); ?>
    <!--header end -->
    
    <div class="container">
    </div>
    
    
     <!--footer start -->
        <?php include("includes/footer.php"); ?>
     <!--footer end -->
    

    require will produce a fatal error (E_COMPILE_ERROR) and stop the script.

    include will only produce a warning (E_WARNING) and the script will continue

    So, If you want the execution to go on and show users the output, even if the include file is missing, use the include statement.