Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

119 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 5 out of 15
Phil

Phil

with Mr. Jones
Status: Offline!

maybe

PHP:


<?php
switch( $HTTP_GET_VARS['what'] ) {
 
   case 
'contact' 
        include 
'contact/index.php';
        break;
    default:
        include 
'main.php';
        break;
}
?>

Not sure if I got the path correct though - I dont know if you need contact folder

___________________

http://www.philbrodeur.com - Expert PHP Development and Tutorials

Phil

Phil

with Mr. Jones
Status: Offline!

Updated - fixed $1337 and the register_globals inconsistence Jeb pointed out, and added the following section.

---

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

___________________

http://www.philbrodeur.com - Expert PHP Development and Tutorials

Last edited by Phil, June 16th, 2003 12:16 AM (Edited 1 times)

CDude

CDude

nothing
Status: Offline!
Quote:

Originally posted by Ares
Updated - fixed $1337 and the register_globals inconsistence Jeb pointed out, and added the following section.

---

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:

PHP:


<?php

if(isset($_GET['news'])) {
     include 
'pages/news.php';
}
if(isset(
$_GET['portfolio'])) {
     include 
'pages/portfolio.php';
}
if(isset(
$_GET['forum'])) {
     
header("Location: forum.php");
}
?>

And thats how you do index.php?news

Use $_SERVER['QUERY_STRING'] instead. I can do this test.php?news&porfolio&forum and it'll load all three pages. The only problem with using query string is that it has to be exactly "news" or "portfolio" hence not allowing you to pass any additional variables to the next page.

___________________

http://celerondude.com

Phil

Phil

with Mr. Jones
Status: Offline!

Fixed. And CDude..the title...is missing a word? Before the :? Wink

___________________

http://www.philbrodeur.com - Expert PHP Development and Tutorials

eli2k

eli2k

Neverside Newbie
Status: Offline!

I'm using the script from here for my page.

Quote:

Originally posted by CDude
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]);

?>

When I load the index.php page, for some reason, the default page and 404 page both show up. The default page is at the top and the 404 page is at the bottom. If I link to index.php?page=default, then it will work. Did I mess up on the coding somewhere? Here's what is looks like....

PHP:


<?php
// what variable in the query string.  ex; index.php?goto=some page
$var_name "page";
// 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' => 'entry.php',
       
'404' => '404.php',
       
'contact' => 'contact.php',
       
'life' => 'life.php',
       
'location' => 'location.php',
);

$var = @$_GET[$var_name];

if(!
$var)
    include(
$pages['default']);
if(!
contact)
    include(
$pages['contact']);
if(!
life)
    include(
$pages['life']);
if(!
location)
    include(
$pages['location']);
elseif(!isset(
$pages[$var]))
    include(
$pages['404']);
else
    include(
$pages[$var]);

?>    

___________________

- Eli

next493

next493

Status: Offline!

it shouldn't be $_GET[$var_name] it should be $_GET['var_name']

___________________

http://pulse.whatnet.org/api/img.php?uid=6084

Adam

Adam

Status: Offline!

actually, either of those will work.

___________________


Current Project: LivermoreMuscle.com
Sig Hosted by Motorspin

underburn

underburn

Status: Offline!
Quote:

Originally posted by Ares
maybe

PHP:


<?php
switch( $HTTP_GET_VARS['what'] ) {
 
   case 
'contact' 
        include 
'contact/index.php';
        break;
    default:
        include 
'main.php';
        break;
}
?>

Not sure if I got the path correct though - I dont know if you need contact folder

I personally need it.... its much simpler for me.

___________________

Signature Suspended as it is in violation with the signature rules

underburn

underburn

Status: Offline!

I figured it out... thanks a million Ares

___________________

Signature Suspended as it is in violation with the signature rules

swish

swish

Status: Offline!
PHP:


<?php

function news() {
print (
"This is the news page");
}

function 
contact() {
print (
"This is the contact page");
}

switch(
$r) {
  case 
1:
    
news();
  break;
  case 
2:
    
contact();
  break;
}
?>

___________________

Graphic and Web Design services at flowtone.com

Page 5 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.0132 seconds.