PHP Dynamic Include
This is, in my opinion, a bit easier and difficult than the normal PHP Include: easier in the code, and harder in the links.
As for the normal PHP Include, you start with an HTML page - for references to that part, use my
PHP Include tutorial.
Once you have the HTML page, put it aside for a moment and, with your chosen editor, create four php files - one more than the classic include - and call them
head.php,
foot.php,
index.php and
main.php.
The first file, head.php, is where the HTML page's header goes; you simply copy the code on your HTML page and paste it on the file. For the file to work, the code has to look like shown here below.
<!DOCTYPE html>
<html>
<head>
<title>page title</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="navigation">navigation here</div>
<div id="content">
Now you do the same copy and past routine with the second file,
foot.php; final code has to look like below.
</div>
</body>
</html>
Here is where Dynamic PHP Include differs from common Include: the
main.php will in fact have no php snippets, but only the site content - like this.
CONTENT GOES HERE, ETC.
The
index.php file will be even more different (and completely different from the classic include's).
Since it's very complicated also for me to copy and paste all index file code here, just download the file called
home.php and rename it to
index;
don't touch anything on that file but the lines I'm gonna indicate you, or you'll end up messing it up and it'll no longer work.
The first line you have to touch is where it says what I show below.
// THIS WAS EXPLAINED IN PHP Includes
include ("head.php");
You change only where it says 'head.php', renaming it head, top or whatever your header file is called.
Then you search where it says the following.
$include = 'main.php';
include ($include);
That is the link it uses to find your content pages.
Last, you go where it says what you see below...
// THIS WAS EXPLAINED IN PHP Includes
include ("foot.php");
...and change only where it says 'foot.php' - to whatever your footer file is called.
The file names are not mandatory; as long as you match the parts I showed you to your files names, you can rename the four files to whatever you want.
Now comes the hard part, meaning the links...which is not that hard once you remember their construction, shown below.
<a href="index.php?page=page name">link name</a>
Always remember to
put the index.php?page= before the page's actual name and
not to add the *.php suffix; if you forget these rules, the links won't work.
There, the
Dynamic PHP Include is done.
NOTE :: To have your full site done with PHP Include, you just have to copy the main.php for how many times how are your pages and fill them with your content.