Publishing System Settings Logout Login Register
Using one page for a whole website - Database Driven
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on September 4th, 2007
6187 views
PHP Coding
What are we trying to do?
Think about this, imagine you created a website where every page was a different file for example you had an index file, a contact me file, a services file and what other page you can think off. What happens if you need to change/add information or design of the pages this means editing each page seperatly which if you have alot of files can take a long time.

Okay so if you want a way around this then here we go!

The Database
Okay first things first we need to make a database to hold all our information for our seperate pages, this database is very simple to create and isn't really complex at all.

all we need are 2 seperate fields; pagename and html

when you are making your datebase make sure that you make the pagename field a primary key as this stops you from having two of the same page it will mess up the system.

if you just want the mysql code then here it is

CREATE TABLE `websitepages` (
`pagename` VARCHAR( 255 ) NOT NULL ,
`html` VARCHAR( 1000 ) NOT NULL ,
PRIMARY KEY ( `pagename` )
) ENGINE = MYISAM ;


okay now that we have our database created lets just insert some data into it so that we can see our code working as we write it, don't worry at this point if the information you enter isnt what you want to be on each of the page this is just for now.

just to save you time you can simply just run this mySQL query to insert 2 values into the database we have created.

INSERT INTO `websitepages` (
`pagename` ,
`html`
)
VALUES (
'index', 'Hello this is the homepage'
), (
'contactme', 'woo i am another page'
);


All i have done is created 2 Pages;

Index
hello this is the homepage

and...

Contactme
woo i am another page

these 2 page i will pretend are 2 pages i want on my website.

The PHP
okay now that the backbone of the website is done now we need to make it work. This part of the tutorial is just as easy to understand so dont worry if you dont know much about PHP as this is a very simple PHP script.

okay so lets create a file and call it index.php

now that this file is created open it up in any text editor for example notepad and copy/write the following;

<?php

$page = $_GET[page];

?>


okay now this piece of code creates a variable which is stores the information $_GET[page] which is located in the addess bar for example www.samplewebsite.com/?page=index , the word index will be saved as the variable $page

other example to understand the $_GET[page] section;

if the address is www.samplewebsite.com/?page=contactus
then the variable would be contactus

if the address is www.samplewebsite.com/?page=bob
then the variable would be bob

Okay so now we know that this line of code we grab information for us to use from the address bar, now what we need to do is take that information and display a web page.

now editing the same file copy/write the following;

<?php

$page = $_GET[page];

$db = mysql_connect("localhost", "database_username", "database_password");
mysql_select_db("database_name",$db);

$query = mysql_query("SELECT * FROM websitepages WHERE pagename='$page'",$db);
$row = @mysql_fetch_array($query);

?>


You WILL have to change the values in the connect statement (which are in bold) to your database information $db = mysql_connect("localhost", "database_username", "database_password");
mysql_select_db("database_name",$db);

this code we have just written/copied takes the information from the address bar and stores it as the variable $page then it connects the database and looks in the database for information with $page as the pagename.

okay now what we simple need to do is display the information that it has found from the database, okay just continue to edit the index.php file and write/copy the following;

<?php

$page = $_GET[page];

$db = mysql_connect("localhost", "database_username", "database_password");
mysql_select_db("database_name",$db);

$query = mysql_query("SELECT * FROM websitepages WHERE pagename='$page'",$db);
$row = @mysql_fetch_array($query);

echo $row[html];

?>


okay so now it will display the infomation it has found from the database so if you typed in www.samplewebsite.com/?page=index it would display the HTML information we inserted into the database before.

Simple.

Okay now you may be thinking okay great but what happens if someone visits a page that isnt there what will happen? well simply what will happen is that php will find no results from the database and display nothing but this isnt always what we want so what we can do is make a little error message like a 404 message appear if this happens.

okay to do this just write/copy the following;

<?php

$page = $_GET[page];

$db = mysql_connect("localhost", "database_username", "database_password");
mysql_select_db("database_name",$db);

$query = mysql_query("SELECT * FROM websitepages WHERE pagename='$page'",$db);
$row = @mysql_fetch_array($query);
$count = mysql_num_rows($query);

if($count > 0){

echo $row[html];

}else{

echo 'Sorry this page does not exist';

}
?>


this code counts the number of rows that have been found in the database from the variable that we have in the address bar and stores that number as the variable $count this is then checked to see if the number is greater then 0, if the variable is greater then 0 this means that a page has been found so we can just display the HTML code otherwise if it isnt greater then 0 this means that a page has not been found so it displays an error message.

okay great now if we where to test it out then then it would display and error message for any page that hasn't been found, o but what you maybe thinking is what happens if someone didnt type in ?page= and just visited www.samplewebsite.com?

Okay at the minute it will display nothing as the variable $page will be blank so it will display the error message which isnt good for someone just visiting your website.

to get around this simple write/copy the following into index.php

<?php

$page = $_GET[page];

if($page == ""){
$page = "index";
}

$db = mysql_connect("localhost", "database_username", "database_password");
mysql_select_db("database_name",$db);

$query = mysql_query("SELECT * FROM websitepages WHERE pagename='$page'",$db);
$row = @mysql_fetch_array($query);
$count = mysql_num_rows($query);

if($count > 0){

echo $row[html];

}else{

echo 'Sorry this page does not exist';

}
?>


This Script now asks if the variable $page equals blank, and if it does then to set it as index which would be the homepage. Simple as that.

Okay Okay the smart people who are reading this might now say "hey! what happens if i want one of my pages to have PHP script on it?" well dont worry there is a way to add this in too

Simple write/copy the following into index.php;

<?php

$page = $_GET[page];

if($page == ""){
$page = "index";
}

if($page == "portfolio")
TYPE PHP SCRIPT HERE
}

$db = mysql_connect("localhost", "database_username", "database_password");
mysql_select_db("database_name",$db);

$query = mysql_query("SELECT * FROM websitepages WHERE pagename='$page'",$db);
$row = @mysql_fetch_array($query);
$count = mysql_num_rows($query);

if($count > 0){

echo $row[html];

}else{

echo 'Sorry this page does not exist';

}
?>


Now same as it did before it asks if the page is portfolio (this can be whatever page you want it to be) and then if it is then run the PHP code that you have typed in, this means you dont have to add this page to the database.

Okay thats it folks, if you have any questions/queries just email me at [email protected] or leave me a message and ill get back to you :D hope this has helpped.

-Sephedo
www.maaku.net
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Sephedo

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top