PDA

View Full Version : PHP: doing something useful.



Carnage
4th May 2006, 09:23 AM
This is something i seem to show lots of people over and over, so i figured id save myself sometime and write it down.

When writing a normal pure html page, you quite often have somethings which appear on every page in your site menus, header images etc. Now, what if you want to change one of these things, say you add a new page in and want to add a new link on the menu. In order to do so, you would have to edit every singal page on your site to put the new link in. This is very time consuming, not to mention that fact that you may not quite get it the same on every page.

So, what are the solutions?

Meny people do this using frames, however i dont think this is the best way to do it also this is a php tutorial so i'm going to ignore this method.

The basis behind the php method is using a simple function, include (http://www.php.net/manual/en/function.include.php)

This takes another php file and adds its contents into the current file automatically.

Now to add in menus etc we need to create them as a singal seperate file



<table>
<tr><td><b>menu</b></td></tr>
<tr><td>url1</td></tr>
<tr><td>url2</td></tr>
<tr><td>url3</td></tr>
<tr><td>url4</td></tr>
</table>


this would then be saved as menu.php

the following could also be saved as header.php



<html><head><title>my website</title></head>
<body>
<table>
<tr><td><h2>My website</h2></td></tr>
</table>


and finially footer.php



<table>
<tr><td>Copyright me 2006</td></tr>
</table>
</body></html>


Then each of your pages would use the following php code:



<?php
include('header.php'); //this includes our header
//we close the php tag so we can output normal html?>
<table>
<tr><td><?php include('menu.php');?></td><td>main content for page goes here</td></tr>
</table>
<?php include('footer.php'); ?>


The finial output of this would be:



<html><head><title>my website</title></head>
<body>
<table>
<tr><td><h2>My website</h2></td></tr>
</table>
<table>
<tr><td><table>
<tr><td><b>menu</b></td></tr>
<tr><td>url1</td></tr>
<tr><td>url2</td></tr>
<tr><td>url3</td></tr>
<tr><td>url4</td></tr>
</table></td><td>main content for page goes here</td></tr>
</table>
<table>
<tr><td>Copyright me 2006</td></tr>
</table>
</body></html>


Dynamically creating each of your pages.