NameSilo

Tutorial: Simple site management with the use of PHP and the query string (?page=1)

Spaceship Spaceship
Watch
Impact
6
Code:
        //
       //  
      //Tutorial by deadserious - © [url]http://www.webdesigntalk.net[/url]
     //© 2003 [url]http://www.webdesigntalk.net[/url] 
    //REPUBLICATION OF THE TUTORIAL REQUIRES OUR PERMISSION.
   //[email protected] 
  //
 //
//
This is a simple tutorial to show you the basics of making site management
easier with the use of PHP and the query string.

Including Files
A simple example of how using includes can make site management easier.
For example a file called header.inc contatins:
<html>
<head>
<title>Title of your page</title>
</head>
<body>
<h7>Header Text</h7>

And your main page named index.php contains:
All of your usual html content here.

And a file called footer.inc contains:
Coypright yoursite, All Rights Reserved. and what ever else you want in your
footer here.

Okay so now we've got three files: header.inc, index.php, and footer.inc

Putting it all together.
In your index.php you include the header.inc and footer.inc files like this:

File index.php
PHP:
<?php
include("header.inc"); 
?>
All of your main html content here.
<?php
include("footer.inc");
?>
</body>
</html>

Now with the examples above the source code would look like this after you call it
up in your browser:
<html>
<head>
<title>Title of your page</title>
</head>
<body>
<h7>Header Text</h7>
All of your usual html content here.
Coypright yoursite, All Rights Reserved. and what ever else you want in your
footer here.
</body>
</html>

Now what makes this nice is if you want to edit your footer or header file all
you have to do is make the change to one file and it will be changed throughout
your entire site. It's alot easier editing only page, especially when you have
hundreds or even thousands of pages. That is just a basic example, but it gives
you the basic idea. You can take it alot further and divide your page up into
as many sections as you want. There's alot more to including files (security)
and more which you'll learn eventually with time and experience, however if you
don't mind people having the ability to view the files your including and
you're not storing any sensitive information in them this way should be safe.
:D Some ways to get around people having the ability to view your included
files is name them something noone would ever guess. Surrounding them with the
start <?php and ?> end tags, and alot of others you'll eventually learn.

Note:You don't have to use the .inc extension for your included files,
you can use .txt, .php, .htm, .html or anything you want.

We'll begin with a simple switch statement to get started and show you how to
make use of the Query String.
File: switch.php
PHP:
<html>
<body>
<?php
switch ($page) 
{
case '1':
echo 'This is page 1';
break;
case '2':
echo 'This is Page 2';
break;
case '3':
echo 'This is Page 3';
break;
default:
echo "Sorry Page $page doesn't exist";
break;
}
?>
</body>
</html>

Now if you call up this url in your browser:
http://www.yourdomain.com/switch.php?page=1

You'll get: This is page 1

If you change the end of the url to ?page=2 you'll get:

This is page 2

And for any request that doesn't end with ?page=1, ?page=2, or ?page=3 you'll
get the default.

So if you called up http://www.domain.com/switch.php?page=4 you'll get:

Sorry Page 4 doesn't exist

Now you can change the $page variable to anything you want. So for example if
you changed it to $cmd then you would call up the url's like:
http://www.domain.com/switch.php?cmd=1

You can also change each case to what ever you want. So for example you could
change the 1st case to something like case 'design':

Then you could call up http://www.domain.com/switch.php?page=design

Everything that follows the question mark in a url is the query string. The
query string works like variable=name and each pair of variable=name is
separated by the & sign.
The query string can be very useful for creating dynamic web pages. PHP makes
it easy to to take advantage of the query string because all the hard stuff has
already been done for you.

Note: Depending on how PHP is set up on your server you may need to
change the first line of the switch statement from:
switch ($page)
to:
switch ($_GET['page'])
or:
switch ($HTTP_GET_VARS['page'])

You'll still be able to change 'page' to what ever you want. We'll use the
($_GET['page']) method in the rest of the examples.

Easy enough right? :D

You can also include files and do various other things based on the query
string rather than just echoing output to the browser.

In this example we have five files.
1. header.inc with the following content for an example:
<title>Fun with PHP</title>
</head>
<body bgcolor="#ffffff">

2. main.inc with:
<h7 align="center">This is the main page</h7>

3. design.inc with:
This is the design page

4. services.inc with
This is the services page

5. footer.inc
<center>Copyright mysite.com</center>
</body>
</html>

Now we have one more file, we'll call it switch.php with the following content:
PHP:
<html>
<head>
<?php
include('header.inc');
switch ($_GET['page']) 
{ 
case 'home': 
include('main.inc'); 
break;
case 'design': 
include('design.inc'); 
break;  
case 'services': 
include('services.inc'); 
break;                                                                                               
default: 
include('main.inc');
break; 
} 
include('footer.inc'); 
?>
</body>
</html>

Now if you look at the source code after you call this up as
http://www.yourdomain.com/switch.php in your browser you'll see the default:
<html>
<head>
<title>Fun with PHP</title>
</head>
<body bgcolor="#ffffff">
<h7 align="center">This is the main page</h7>
<center>Copyright mysite.com</center>
</body>
</html>

If you change the url to http://www.yourdomain.com/switch.php?page=home you'll
get the same thing as the default.

If you change the url to http://www.yourdomain.com/switch.php?page=design
you'll get:
<html>
<head>
<title>Fun with PHP</title>
</head>
<body bgcolor="#ffffff">
This is the design page
<center>Copyright mysite.com</center>
</body>
</html>

Now you see how the design.inc file was included in the main part of the page
replacing the main.inc page? :D
The same will work with http://www.yourdomain.com/switch.php?page=services etc.

You can also do the exact same thing using if elseif statements.
PHP:
<?php
if ($page == 'design') {
echo 'This is the design page';
} elseif  ($page == 'home') {
include('main.inc');
} else {
echo "Page $page doesn't exist";
}
?>

Using these methods you could manage hundreds or even thousands of pages from a
single file. There's alot of different ways to go about it which you'll learn
eventually if you stick with PHP, which of course you will. :D Well that should
give you a basic idea of how you can make site management simple with the use
of PHP. Feel free to correct any mistakes I may have made, post your comments,
suggestions, and ask any questions you may have.

Hint: Want to get rid of the page variable /?page=design and have it called as
/?design ...... try using ($_SERVER['QUERY_STRING']) :D
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Im pretty new to php and was looking and thinking of ways to make management easier, thanks for the help.

Although i plan on taking it a small step further, I saw a guy use skins on his site. skin grey, turned everything grey and changed the head image. He had 2 more skins, i thought that it was a great idea. Using that switch function will make my life easier. thanks alot
 
0
•••
Cool! Glad you found it useful. :D
 
0
•••
Thanks for a great basic primer in PHP that is very useful. You won't believe how hard it is to explain this stuff to people learning PHP. Now I can show them your tutorial!

:)
 
0
•••
Just what i was looking for.. Thanks alot! Easy to follow and helped me understand how to do it.
 
Last edited:
0
•••
Brilliant Posts, glad its been made a sticky.
 
0
•••
Hey, really cool. Congrats on the sticky
 
0
•••
excelent tutorial! extremely informative and easy to follow.
thanks your time and effort.
 
0
•••
Nice guide. Only one gripe :p
its a lot, not alot... two words :D
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back