The include statement

The include() statement includes and evaluates the specified file, it lets you insert a page or more in a single chosen php page. It is like working with frames in html, for example:

Syntax

include (‘page.php’);
//or
include ‘page.php’;

Create a simple php template with menu

The Home page index.php:

<html>
<head><title>My website title</title>
</head>
<body color="#000000">
<table style="width:750px;margin-left:auto;margin-right:auto">

<tr>
<td style="width:150px; vertical-align:top">
<?php
//we insert the menu page here
include 'menu.php';
?>
</td>
<td style="width:600px;vertical-align:top">

<?php
//we insert the content page here
include 'content.php';
?>
</td>
</tr>
</table>

Attention: when you insert a page, it should not contain HTML tags as <html> <head> <body> only table and images, or simply the content of the page, otherwise you’ll have several times in the html tag Your Page!

The menu.php:

<table style="width:150px;margin-left:auto; margin-right:auto; ">
<tr>
<td style="text-align:center"><a href="#">Home</a></td>
</tr>
<tr>
<td style="text-align:center"><a href="#">Services</a></td>
</tr>
<tr>
<td style="text-align:center"><a href="#">Contact</a></td>
</tr>
</table>

I will leave you to do the content.php by yourself, do as we did for the menu page.


Now lets do something that will interest you. probably already seen sites like this for example, with page that does not move and only urls that get same change like: http://www.domain.com/?page=news you want to know how. I will explain to you the operation below.
We will use the same index.php page

Lets do some changes to do menu.php page:

<table style="width:150px; margin-left: auto ; margin-right: auto">
<tr>
<td style="text-align:center">
<a href="/?page=news">News</a> </td>
</tr>
<tr>
<td style="text-align:center">
<a href="/?page=guestbook">Guesbook</a> </td>
</tr>
<tr>
<td style="text-align:center">
<a href="/?page=contact">Contact Us</a> </td>
</tr>
</table>

I made the links with an anti-slash (/) but you can replace with index.php?page=news, because behind the (/) it is the index.php that’s hided. but I think that you knew it.

Now what our content.php should looks like; here we will need to use conditions:


<?php
//if the variable equal to news
if($_GET['page'] == 'news'){
//we insert news page
include('news.php');
}
//else if the variable equal to guestb
elseif( $_GET['page'] == 'guestbook'){
//we insert the guestbooh.php
include('guestbook.php');
}
//else if the variable equal to contact
elseif( $_GET['page'] == 'contact'){
//we insert the guestbooh.php
include('contact.php');
}
?> 

Caution: If the called page is in a sub-folder; think to call it with the full path, if for example the news page is in a sub-folder called pages, so the include statement should be like include ‘pages/news.php’; .

If you have too many pages, I suggest that you use the switch operator it’s so easy to handle as many pages as you want without getting confused with the if and elseif.
This is an example of how to use switch instead of if and elseif.

<?php
switch($_GET['page'])
{

case'news':

include('news.php');
break;

case'guesbook':
include('guesbook.php');

break;

case'contact':
include('contact.php');
break;

default:

//Always put a default page that your visitor...
//will see if the variable is not assigned
include ('home.php');

}
?>
Filed under PHP and MySQL. You can follow any responses to this entry through the RSS 2.0. You can leave a response by filling following comment form or trackback to this entry from your site

PHP and MySQL' CATEGORY »

Introduction to PHP
Get started with PHP
PHP variables
PHP variables of environment
PHP conditions
PHP Looping
PHP Cookies
PHP working with dates
PHP working with arrays
PHP working with files
PHP play with strings
PHP and Forms
Send emails with PHP
The include statement
Get started with MySQL
MySQL update and delete
The WHERE clause
MySQL functions
Simple PHP guestbook script
Simple PHP websites directory script
Multiple pages with PHP
Create your forum with php

No Responses for “Create your forum with php”

  1. robert says:

    thnk u for the good tutorial

Leave a Reply