Tutorial: page.php?page=blah / query strings / easy template tutorial (UPDATED)
Hopefully, this will cut down on the incredible number of requests we get for this. First, a couple of key terms: 'query string' is the ?page=blah part in 'page.php?page=blah'. page=blah is called a variable=value pair. You can seperate them by doing 'page.php?var1=bob&var2=tom'.
Now, something that will cause many of you problems is register_globals. If its off (which it is by default in the last few php distrubs), using $queryStringVar when going to page.php?queryStringVar=tom wont print out anything. In fact, chances are it will give you an unset / undefined variable error... whcih isnt good! But we have a soultion.
Remember the variable=value pairs?
We can get things from the query string through the $_GET array, no matter what. So since $queryStringVar doesnt work, we have to get it through $_GET['queryStringVar'].
The general idea is this:
If you go to 'page.php?color=red&money=37&var1337=sure', then we DONT get it by this:
PHP:<?php
print $color . '<br />';
print $money . '<br />';
print $var1337 . '<br />';
?>
Instead, we use the $_GET array!
PHP:<?php
print $_GET['color'] . '<br />';
print $_GET['money'] . '<br />';
print $_GET['var1337'] . '<br />';
?>
This works, and is proper coding. HAPPY DAY.
Now, we have the all to oft template/ include problem:
Things that DONT work"
PHP:<?php
if($page == 'one') {
include 'page1.php';
}
?>
AND
PHP:<?php
include $page;
?>
Instead, were going to use the switch statement and the $_GET array!
PHP:<?php
switch( $_GET['page'] ) {
case 'news' :
include 'news.php';
break;
case 'portfolio' :
include 'portfolio.php';
break;
case 'bush' :
include 'go_to_war_with_iraq_for_no_reason.php';
break;
case 'me' :
print 'war sucks... GO ANTI WAR ME';
default:
include 'news.php';
break;
}
?>
Isnt that easy to read? It is, isnt it. Thats why you should use it. Feel free to ask questions...here.
new as of june 15:
Now, you may ask - what about index.php?news / index.php?portfolio means of navigation? This actually isn't that hard. You see, when you go to index.php?variable, variable is initialized but not given a value. That means you can do this:
Way one, if you intend to have other query string variables besides just ?page
PHP:
<?php
if( isset($_GET['news']) and !defined('PAGE_LOADED') ) {
include 'pages/news.php';
define ('PAGE_LOADED', '');
}
if( isset($_GET['portfolio']) and !defined('PAGE_LOADED') ) {
include 'pages/portfolio.php';
define ('PAGE_LOADED', '');
}
if( isset($_GET['forum']) and !defined('PAGE_LOADED') ) {
define ('PAGE_LOADED', '');
header("Location: forum.php");
}
?>
And if you don't intend to have other query string variables:
PHP:
<?php
if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] == 'news') {
include 'pages/news.php';
}
if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] == 'portfolio') {
include 'pages/portfolio.php';
}
if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] == 'forum') {
header("Location: forum.php");
}
?>
And thats how you do index.php?news
links:
switch control structure
Register globals
___________________
http://www.philbrodeur.com - Expert PHP Development and Tutorials
Last edited by Phil, June 16th, 2003 12:17 AM (Edited 1 times)





