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:
include (‘page.php’);
//or
include ‘page.php’;
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.
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');
}
?>
thnk u for the good tutorial