HowTo – Using Includes To Divide PHP Pages Into Manageable Sections

Filed under: Web Design

phpWhen designing your website it is a great idea to turn your template design into manageable sections by separating your header, footer, sidebars and maybe menus or other constant areas.

The idea is that headers and footers will not often change.

The same is true for sidebars that often have the same content across your site… maybe a search box and a couple ads or a link to a script that shows content on all pages of your site.

Site Wide Menus if you are generating them manually will need to be updated every time you add a page to the site and editing one menu.php file instead of every page on your site when you add a new page to the menu is a lot easier.

Context menus are different if they are only used on a few pages or if they are use in categories. In this case you may want to make a context menu for each category of pages that you have.

Anyway the idea is to create a skeleton template that you will use for adding new pages to your site and within that template you will only put content specific to that page.

You will then include these other files to display your header footer menus and sidebars.

an example might look something like this (and yea we’re going to use tables for ease of idea)

Page Skeleton page.php

<Doctype>
<html><head><title>mypage</title></head>
<table>
<tr><td><?php include=(/includes/header.php);></td></tr>
<tr><td><?php include=(/includes/leftsidebar.php);></td>
<td> your content would be here </td></tr>
<tr><td><?php include=(/includes/footer.php);></td></tr>
</table>
</body></html>

header.php

<table>
<tr><td><img src=”logo.jpg”></td><td>SearchBox</td></tr>
</table>

sidebar.php

<table><tr>
<td width=”160″>
<searchbox code>
MENU
<a link>page1</a><br />
<a link>page2</a><br />
<a link>page3</a><br />
<a link>page4</a><br />
<adbanner code>
</td></tr>
</table>

This is the basic concept split the page into sections that you can modify independently then assemble the viewable pages by using includes inside a skeleton page.

Your use will probably differ in layout and it will take some trial and error to get everything looking right.

Note
It is always better to use a file based include instead of a http based one because even though the files are local to your server they will count against your traffic. Some hosts even restrict includes over http.