Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

76 users online



Tutorial: page.php?page=blah / query strings / easy template tutorial (UPDATED)

Tutorial: page.php?page=blah / query strings / easy template tutorial (UPDATED)

Currently viewing this thread: 1 (0 members and 1 guests)


Page 1 out of 15
Phil

Phil

with Mr. Jones
Status: Offline!

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)

epyon

epyon

16, staying Asian.
Status: Offline!

nice, i will link this from the sticky.

___________________

http://whatpulse.bounceme.net/sig/epyon.png

Jomdom

Jomdom

Status: Offline!
PHP:

<?php

    
case 'bush' :
        include 
'go_to_war_with_iraq_for_no_reason.php';
        break;
    case 
'me' :
        print 
'war sucks... GO ANTI WAR ME';

?>


Priceless Smile

___________________

http://dscosmetics.com/storage/dsc2.0.jpg

http://dscosmetics.com/storage/brokensig.jpg

Motorspin

Motorspin

Neverside Newbie
Status: Offline!

lol agreed jomdom

___________________

Travis Farrell
http://motorspin.com/stuff/images/avasig/m_enterprisesig.gif

epyon

epyon

16, staying Asian.
Status: Offline!
PHP:

<?php
if(strstr($post"iraq")) {
    echo 
"not need here. go post in the debate forum or something.\n";
}
?>

___________________

http://whatpulse.bounceme.net/sig/epyon.png

Zober

Zober

Status: Offline!

sorry but what if i wanted to put html in here, would I just

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 
'<img src='www.whitehouse.com/bushy.jpg'>';
        print 
'<center>Bush having fun :P</center>';
    default:
        include 
'news.php';
        break;
}

?>

also, could somebody in explain to me, in english :P, what is the difference between the variables, print, include, echo?

Thank you very much
~Zober

inigoesdr

inigoesdr

Status: Offline!

It would be best to put your html in another page and include it.

Include: Outputs the contents of the page that is included

Echo: Outputs Text

Print: Basicly The Same as Echo

CDude

CDude

nothing
Status: Offline!

for the people that didn't read the other threads

PHP:

<?php
// what variable in the query string.  ex; index.php?goto=some page
$var_name "goto";
// list of your pages, what you want to call it and what file should be included.
// example 'pictures' => '/home/url/public/page/that/is/not/in/the/same/directory',
// default is the default page, duh!
// 404 is the page to load when the user wants a  page that does not exist.  404.php
// can be a file that stores the URL into a text-file so you know if you have any broken links.
$pages = array(
       
'default' => 'main.php',
       
'404' => '404.php',
       
'pic' => 'what.php',
       
'page name' => 'whatever.asdfsdfdsfsdf',
);

$var = @$_GET[$var_name];
if(!
$var)
    include(
$pages['default']);
elseif(!isset(
$pages[$var]))
    include(
$pages['404']);
else
    include(
$pages[$var]);

?>

___________________

http://celerondude.com

RedLeo

RedLeo

Status: Offline!

Yeah, I'm sorta stuck here. i put the code in my page (articles.php) like so:

PHP:

<?php
switch( $_GET['article'] ) {
    case 
'articone' 
        include 
'stupidpeople.php';
        break;
    default:
        include 
'articlesmain.php';
        break;
}
?>


But when I go to articles.php?article=articone, it includes the default (articlesmain.php) instead of stupidpeople.php. Man, this PHP stuff is harder than it looks.

___________________

I like pie.

Jeb

Jeb

Status: Offline!

Try replacing $_GET with $HTTP_GET_VARS.

If that doesn't work, I'm stumped.

___________________

Adam Goossens -- PHP is my mother tounge.

Linux: ( kernel.org | winehq ) -- f33l the p0w3r.

Nobody replying to your questions? Getting flamed? Getting told to RTFM? Ask your questions the right way.

Last edited by Jeb, March 24th, 2003 09:29 PM (Edited 1 times)

Page 1 out of 15
Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0235 seconds.