3

I see multiple answers about creating php files in order to reuse headers/footers, but nothing specific enough. I can't seem to get it to work.

What exactly would the php file look like and what exactly would my html file look like (given the code as it currently is, below)? do I have to convert all my html files to php files in order to use the php include line?

<div id="footer2-wrap">  
    <div class="container">  
        <table id="header">  
            <tr>  
                <td class="phone-number"><span class='wsite-text wsite-phone'>Copyright 2015 | xxx Corporation</span></td>
            </tr>

                    </table>
    </div>

user4378184
  • 53
  • 2
  • 5
  • change the html file to php file include it or use twig and render it – Ajit Kumar Dec 19 '14 at 21:03
  • You don't have to rename your html files as PHP although that is one solution. However your web server does then need to be configured to treat html files as PHP for the include to work. – Dijkgraaf Dec 19 '14 at 21:03
  • i'm sorry can you guys elaborate? i have no idea what twig is, what rendering it would entail, or how to configure my web server to treat html files as php... what would the php file look like? does it start with
    ?
    – user4378184 Dec 19 '14 at 21:06

6 Answers6

3

Create a new file with your footer data. Let's give it the name: footer.php:

<div id="footer2-wrap">  
    <div class="container">copyright etc...</div>
<div>

Then inside your master template (or index.php file), include the footer file:

include_once('footer.php');
Bas van Dorst
  • 6,632
  • 3
  • 17
  • 12
  • so I created the footer.php file, exactly as shown above, and removed that text from my index.php file and replaced it with "include_once('footer.php');" (again, exactly as shown above). Now on the page, the footer is gone (as expected) and the text "include_once('footer.php');" appears at the bottom of the page... – user4378184 Dec 20 '14 at 00:48
2

You should work with include(), require() or require_once functions in PHP to include your files, which depends on your situation.

For instance, let's assume you have a basic php file, called index.php and you want to add sidebar.php, footer.php, navigation.php.

index.php

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css"/>
</head>
<body>
    <?php 
    include("sidebar.php");
    include("navigation.php");
    include("footer.php"); // we include footer.php here. you can use .html extension, too.
    ?>
</body>
</html>

footer.php (or html)

<a href="">About us</a>
<a href="">Our work</a>
<a href="">Testimonials</a>
<a href="">What we do</a>
<a href="">Contact us</a>
salep
  • 1,332
  • 9
  • 44
  • 93
2

Yes, you must have a .php file to use PHP, your server must also have PHP installed (but most already do). PHP also adds to the loading time of a page, so take that into consideration when using it. To add the footer with PHP, you can use the PHP function include(), or, I am not sure if this is considered correct, with file-get-contents():

include():

<?php 
    include("footer.html");
?>

file-get-contents():

<?php
$footer = file_get_contents('footer.html');
echo $footer;
?>

You could also do the same thing with JavaScript:

var xmlhttp, text;
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', 'http://www.example.com/file.txt', false);
xmlhttp.send();
text = xmlhttp.responseText;
document.getElementById("footer").innerHTML=text;

Code taken from here.

Note: The file must be on the same domain to use JS.

Community
  • 1
  • 1
Jacob G
  • 13,762
  • 3
  • 47
  • 67
1

First create a file called menu.php(if you use .html it wont work) On that php file write the html code for your menu

<div id="navbar">
more code
</div>


At your main html file write 
<?php 
    include("menu.php");
?>
stranger4js
  • 269
  • 4
  • 15
  • I tried this as well; nothing shows up... I looked at the source code and my php lines were automatically commented out for some odd reason... – user4378184 Dec 20 '14 at 01:48
0

The code should be able to run. Make sure you are running the code via Apache. Download Xammp or wammp. Start Apache, copy paste your project folder to the xammp folder under httdocs. Then on your browser type in localhost/[your project name] and make sure the html files are changed to .php.

Armali
  • 18,255
  • 14
  • 57
  • 171
David Innocent
  • 606
  • 5
  • 16
0

if you want to pass arguments to it, you can do this:

file1.php

echo $my_var;

file2.php

$my_var="hello world";
include_once('file1.php');
GkAm1
  • 31
  • 2