NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Webmaster Tutorials
Reload this Page Tutorial: Simple site management with the use of PHP and the query string(?page=1)

Webmaster Tutorials Instructional webmaster-related how-to's and tutorials.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 03-21-2003, 01:01 AM THREAD STARTER               #1 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



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


Code:
        //
       //  
      //Tutorial by deadserious - © http://www.webdesigntalk.net
     //© 2003 http://www.webdesigntalk.net 
    //REPUBLICATION OF THE TUTORIAL REQUIRES OUR PERMISSION.
   //webmaster@webdesigntalk.net 
  //
 //
//
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
????: NamePros.com http://www.namepros.com/webmaster-tutorials/14664-tutorial-simple-site-management-use-php.html
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 Code:
<?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
????: NamePros.com http://www.namepros.com/showthread.php?t=14664
you're not storing any sensitive information in them this way should be safe.
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 Code:
<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?

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 Code:
<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?
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 Code:
<?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. 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'])
deadserious is offline  
Old 11-20-2003, 09:08 PM   #2 (permalink)
New Member
Join Date: Nov 2003
Posts: 21
zoroko is an unknown quantity at this point
 



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
__________________
whats green with wheels?


grass, i lied about the wheels, dumbass.
zoroko is offline  
Old 11-21-2003, 07:03 PM THREAD STARTER               #3 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



Cool! Glad you found it useful.
deadserious is offline  
Old 03-22-2004, 01:38 AM   #4 (permalink)
NamePros Expert
 
Anthony's Avatar
Join Date: Dec 2003
Location: NYC
Posts: 9,134
Anthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant future
 

Member of the Month
October 2004

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!

Anthony is offline  
Old 04-09-2004, 09:22 PM   #5 (permalink)
NamePros Regular
 
JNav's Avatar
Join Date: Dec 2003
Location: Australia
Posts: 811
JNav will become famous soon enoughJNav will become famous soon enough
 



Just what i was looking for.. Thanks alot! Easy to follow and helped me understand how to do it.
Last edited by insanity; 04-09-2004 at 10:08 PM.
JNav is offline  
Old 04-22-2004, 09:56 AM   #6 (permalink)
NP Maniacâ„¢
 
Phoenix's Avatar
Join Date: Apr 2004
Location: Nottingham, UK
Posts: 820
Phoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to behold
 




Brilliant Posts, glad its been made a sticky.
__________________
PHP/MySQL Development offered: PM me for a quote.
47ideas.com - my freelancing portfolio.
Phoenix is offline  
Old 05-31-2004, 09:02 AM   #7 (permalink)
Senior Member
 
matrics's Avatar
Join Date: Apr 2004
Posts: 1,672
matrics is a glorious beacon of lightmatrics is a glorious beacon of lightmatrics is a glorious beacon of lightmatrics is a glorious beacon of lightmatrics is a glorious beacon of lightmatrics is a glorious beacon of lightmatrics is a glorious beacon of lightmatrics is a glorious beacon of light
 



Hey, really cool. Congrats on the sticky
__________________
█ █ █ My T-shirt Addiction!!!
Hero's Gold
matrics is offline  
Old 06-22-2004, 01:45 AM   #8 (permalink)
NamePros Member
 
whyme953's Avatar
Join Date: Jun 2004
Posts: 92
whyme953 is an unknown quantity at this point
 



excelent tutorial! extremely informative and easy to follow.
thanks your time and effort.
__________________
Bais Menachem
whyme953 is offline  
Old 10-09-2004, 02:20 PM   #9 (permalink)
Senior Member
 
Fusionhost's Avatar
Join Date: Aug 2003
Location: Plymouth, UK
Posts: 1,000
Fusionhost is on a distinguished road
 



Nice guide. Only one gripe
its a lot, not alot... two words
Fusionhost is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 10:08 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger