Newbie Question: multiple pages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
thomasd1
Forum Commoner
Posts: 80
Joined: Sat Nov 22, 2003 2:48 pm
Location: Belgium

Newbie Question: multiple pages

Post by thomasd1 »

hi,
i've seen it lot's a times, these php pages that have links like, main.php?id=home and main.php?id=about ... or something in the menu

what exactly happens here, and what's it good for? :roll: :?:
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

Well in your example you can get the id value.

Code: Select all

<?php

$id = (isset($_GET['id'])?$_GET['id']:"");

?>
If you have that code in main.php
Pash
Forum Newbie
Posts: 7
Joined: Mon Jan 05, 2004 4:30 am

Post by Pash »

its basically passing a value of home to the linked page using the variable $id.

you dont normally have to extract the variable values unless there are multiple values from etc.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

As simple demonstration:

Code: Select all

<?php
// index.php.
if (isset($_GET['id'])) {
    switch($_GET['id']) {
       case 0:
           echo 'Welcome to my homepage!';
           break;
       case 1:
           echo 'My name is Foo';
           break;
       default:
           echo 'Nope, no such page here...';
    }
}
?>
<br />
<a href="index.php?id=0">Homepage</a> | <a href="index.php?id=1">I am...</a>
User avatar
thomasd1
Forum Commoner
Posts: 80
Joined: Sat Nov 22, 2003 2:48 pm
Location: Belgium

Post by thomasd1 »

and what's it good for :?:
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Well...

To ease up on how to handle the sites. Take this page: viewtopic.php?t=16324

The 't' stands for topic, so that the viewtopic.php page knows what id to search for in the database to retrieve it's information.

'main.php?id=home' might tell the main.php page to include the home(.php/.html) page somewhere within. By doing this, you can rearrange the looks of the site at instant while having fewer files to edit.

Hopefully clearer? ;)
stylezeca
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2004 4:27 pm

Post by stylezeca »

<?
switch($id) {
default:
include('home.txt');

break; case "about":
include('about.txt');

break; case "contact":
include('contact.txt');
?>


now what this is doing is just including a text file for each case.
if you were to set the link to ?id=about. it would include the about.txt page

you would place that code where you would want the content to show up, like putting that in place of an iframe or something...
pac604
Forum Newbie
Posts: 5
Joined: Tue Jan 27, 2004 11:18 pm

Post by pac604 »

Alright.. I use the one that JAM said. Well, so I put that on my index.php. How will I let the index.php know which files are the ones that's suppose to be id? And can't I change the id to something else?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I think this is the best way to go about this

Code: Select all

<?php
      $id= $_REQUEST['id']; 
      if($id) 
      { 
            include($id . ".php"); 
      } else 
      { 
            echo "page does not exist"; 
      } 
?>
in your links just set it to like ?$id=home , or ?$id=page1
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Doing something like:

Code: Select all

<?php
include($id . ".php");
?>
is generally frowned upon. Having the .php there helps but without what if you did index.php?id=../etc/passwd? A better way to go about this is to create an array with all the valid names in it and check the $id against that array before including the file.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

That is true.....

another possiblity is

Code: Select all

<?php
<?php 
      $link = $_REQUEST['link']; 
      if ( $link == "2" ) { 
	  include("link1.php");
      } elseif ( $link == "3" ) { 
	  include("link2.php"); 
	  } elseif ( $link == "4" ) { 
      include("link3.php"); 
      } elseif ( $link == "5" ) { 
	  include("link4.php"); 
	  } elseif ( $link == "6" ) { 
	  include("link5.php"); 	  
	  } elseif ( $link == "7" ) { 
	  include("link6.php");  
	  } elseif ( $link == "8" ) { 
	  include("link7.php"); 
	  } else { 
	  echo "Page not found 404"; 
	  } 
?>
?>
Post Reply