Page 1 of 1
Newbie Question: multiple pages
Posted: Mon Jan 05, 2004 4:37 pm
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?

Posted: Mon Jan 05, 2004 4:53 pm
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
Posted: Mon Jan 05, 2004 5:12 pm
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.
Posted: Mon Jan 05, 2004 8:44 pm
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>
Posted: Tue Jan 06, 2004 10:52 am
by thomasd1
and what's it good for

Posted: Tue Jan 06, 2004 11:42 am
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?

Posted: Tue Jan 06, 2004 2:06 pm
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...
Posted: Fri Jan 30, 2004 11:26 am
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?
Posted: Fri Jan 30, 2004 1:51 pm
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
Posted: Fri Jan 30, 2004 1:57 pm
by DuFF
Doing something like:
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.
Posted: Fri Jan 30, 2004 2:01 pm
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";
}
?>
?>