PHP include - PHP & MySQL tutorial
- Added: Feb 27th, 2005
- Level: Easy
- Author: Melfina
- Reads: 29,493
- Description: How to use includes to change your layouts simply and saving a lot of time.
Transforming your html into php using includes will save you a whole lot of time everytime you decide to change your layout or simply want to change something from the one you have. You won't be able to view php files on your computer unless you have php installed in it, which is most likely not to happen.
No previous php knowledge is required as it's really simple, but html is, of course, because your page will be done in html normally. What we do with php include is split the layout into three parts (one for the top code, other for the content and the other one for the bottom code) and input the common parts with a simple php code. This is quite similar than what you do when you use an external stylesheet, instead of having it on the same html, you make a css file and call it with: <link href="style.css" rel="stylesheet" type="text/css">.
Now into the tutorial. Once you've got your layout done in normal html, open it in Notepad or similar. I'll be guiding you with an example. Let's say I've got a page like this one:
<html>
<head>
<title>My site</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<table border="0" width="90%">
<tr>
<td width="20%">
<p>Navigation</p>
<p><a href="#">Link here</a><br>
<a href="#Link here</a><br>
<a href="#">Link here</a><br>
...</p>
</td>
<td width="80%">
<!-- Here's where your content starts -->
<p>Hello, welcome to my site, here's where I put all my content :)</p>
<!-- Here ends your content -->
</td>
</tr>
</table>
</body>
</html>
Everything above "Here's where your content starts" and under "Here ends your content" will be the same for every page of your site. We'll make a new document in notepad and take the top part. This is what I would have, for example:
<html>
<head>
<title>My site</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<table border="0" width="90%">
<tr>
<td width="20%">
<p>Navigation</p>
<p><a href="#">Link here</a><br>
<a href="#Link here</a><br>
<a href="#">Link here</a><br>
...</p>
</td>
<td width="80%">
<!-- Here's where your content starts -->
We save this file as header.php. Now back to your complete layout, we'll take the bottom part as we did with the top, something like this:
<!-- Here ends your content -->
</td>
</tr>
</table>
</body>
</html>
We save this as footer.php. We'll be doing the include now. Take your content for the main page, in this case it would be:
<p>Hello, welcome to my site, here's where I put all my content :)</p>
To include the php files we use this code <?php include('header.php'); ?>. We need to add this on the very first line of your code to include the top part and on the last one for the bottom code. We would have something like this in the end:
<?php include('header.php'); ?>
<p>Hello, welcome to my site, here's where I put all my content :)</p>
<?php include('footer.php'); ?>
We save this as index.php and upload all files as well as the ones you need for your layout (images, css...), then we point our browser to your url and it's there! everything shows now.
It was easy, wasn't it? now we just put those php include codes on the top and bottom of every file we want to show the layout in and it will show perfect. When we want to change the layout, we just need to change header.php and footer.php now. Have fun with it.
If you like this tutorial why not digg it or
add to del.icio.us?
Tutorial © Melfina. Do not reproduce in any way without previous permission from the author.