The include statement
Last update on: 10-10-2008The 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';
the html code of 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"> //we insert the menu page here include 'menu.php'; </td> <td style="width:600px;vertical-align:top"> //we insert the content page here include 'content.php'; </td> </tr> </table>
<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
<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:
//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'); }
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
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'); }
PHP and MySQL's lessons:
Introduction To PHPGet 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
Guestbook Script
Websites Directory Script
Multiple Pages With PHP
Create Your Forum With Php

