[advanced search]
Results from the most recent live auction are here.
29 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Webmaster Tutorials
User Name
Password

Old 03-21-2003, 12:01 AM   · #1
deadserious
Senior Member
 
Trader Rating: (13)
Join Date: Aug 2002
Posts: 1,300
NP$: 2.85 (Donate)
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
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
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'])


Please register or log-in into NamePros to hide ads
deadserious is offline   Reply With Quote
Old 11-20-2003, 08:08 PM   · #2
zoroko
New Member
 
Trader Rating: (0)
Join Date: Nov 2003
Posts: 21
NP$: 33.00 (Donate)
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   Reply With Quote
Old 11-21-2003, 06:03 PM   · #3
deadserious
Senior Member
 
Trader Rating: (13)
Join Date: Aug 2002
Posts: 1,300
NP$: 2.85 (Donate)
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
Cool! Glad you found it useful.
deadserious is offline   Reply With Quote
Old 03-22-2004, 12:38 AM   · #4
Anthony
Permanent Resident
 
Anthony's Avatar
 
Name: Anthony
Location: NYC
Trader Rating: (142)
Join Date: Dec 2003
Posts: 9,203
NP$: 26.71 (Donate)
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   Reply With Quote
Old 04-09-2004, 08:22 PM   · #5
insanity
NamePros Regular
 
Name: James
Location: Australia
Trader Rating: (4)
Join Date: Dec 2003
Posts: 762
NP$: 159.00 (Donate)
insanity is on a distinguished road
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 09:08 PM.
insanity is offline   Reply With Quote
Old 04-22-2004, 08:56 AM   · #6
Phoenix
NP Maniac™
 
Phoenix's Avatar
 
Name: Tom
Trader Rating: (29)
Join Date: Apr 2004
Posts: 627
NP$: 21.75 (Donate)
Phoenix is a glorious beacon of lightPhoenix is a glorious beacon of lightPhoenix is a glorious beacon of lightPhoenix is a glorious beacon of lightPhoenix is a glorious beacon of lightPhoenix is a glorious beacon of light
Brilliant Posts, glad its been made a sticky.
__________________
~ Thomas Cameron
Phoenix is offline   Reply With Quote
Old 05-31-2004, 08:02 AM   · #7
matrics
MAGISTER MVNDI SVM
 
matrics's Avatar
 
Name: Vlad
Trader Rating: (32)
Join Date: Apr 2004
Posts: 1,708
NP$: 195.76 (Donate)
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 light
Hey, really cool. Congrats on the sticky
__________________
█ █ █ My T-shirt Addiction!!!
Hero's Gold
matrics is offline   Reply With Quote
Old 06-22-2004, 12:45 AM   · #8
whyme953
NamePros Member
 
whyme953's Avatar
 
Trader Rating: (0)
Join Date: Jun 2004
Posts: 92
NP$: 183.00 (Donate)
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   Reply With Quote
Old 10-09-2004, 01:20 PM   · #9
hilljd00
Namepros Glower™
 
hilljd00's Avatar
 
Name: Jon
Location: UK
Trader Rating: (6)
Join Date: Aug 2003
Posts: 965
NP$: 118.00 (Donate)
hilljd00 is on a distinguished road
Nice guide. Only one gripe
its a lot, not alot... two words
__________________

Nothings Impossible, Only a challenge..
hilljd00 is offline   Reply With Quote
Closed Thread

NamePros is a revenue sharing forum.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Site Sponsors
http://www.internetinvestments.com/ http://www.dnfinder.com Hunting Moon
Advertise your business at NamePros
All times are GMT -7. The time now is 03:26 PM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0