Page 1 of 2
how to make this?<http://something.com/index.php?id=1&
Posted: Fri Oct 10, 2003 7:58 pm
by gene
i want to know how ppl can do that
http://something.com/index.php?id=1&c=main
Posted: Fri Oct 10, 2003 8:15 pm
by nigma
I tried to think of a way to explain it, but I couldn't. So here is what the code I use to do that:
Code: Select all
<?php
define(TOPOFPAGE, "topofpage.html");
define(BOTTOMOFPAGE, "bottomofpage.html");
$validPages = array (
"page1.php",
"page2.php",
"page3.php"
);
(int)$id = $_GET['id'];
include_once(TOPOFPAGE); // Include the html for the top of the page
if (!empty($validPages[$id])) // If the id entered is valid then include the apropriate page
{
include_once($validPages[$id]);
}
else // If the id they entered is not valid then include the main page
{
include_once('mainPage.php');
}
include_once(BOTTOMOFPAGE); // Include the html for the bottom of the page
?>
You should also check some other things like whether the file exists but I removed those functions for clearity.
BTW, I defined the TOPOFPAGE and BOTTOMOFPAGE so that I can easily change which pages to include as the top and bottom.
Posted: Fri Oct 10, 2003 8:18 pm
by gene
if i want to link the file page1.php
what should i do?
Posted: Fri Oct 10, 2003 8:21 pm
by nigma
Posted: Fri Oct 10, 2003 8:26 pm
by gene
http://www.something.com/index.php?id=0&
c=123
how about this?how can i do that?
sorry for too many questions...because i am newbie in php...
Re: how to make this?<http://something.com/index.php?id=1
Posted: Fri Oct 10, 2003 8:55 pm
by Gen-ik
In your example the 'id' and 'c' will be created in PHP as variables..... here's an example..
Let's say you use this link..
index.php?id=1
Your index.php page might look something like this.....
Code: Select all
<html>
<body>
<?
if(isset($id)) // checks if 'id' was part of the link
{
if(file_exists("mypages/page".$id.".php")) // checks if the page exists
{
include("mypages/page".$id.".php"); // includes the page
}
else
{
echo("Page not found."); // gets shown if the page doesn't exist
}
}
else
{
echo("No page requested."); // gets shown if 'id' wasn't part of the link
}
?>
</body>
</html>
If you're just starting to learn PHP though don't jump in at the deep end!
Posted: Fri Oct 10, 2003 10:43 pm
by nigma
SIDE NOTE
Gen-ik's example won't work if register_globals is turned off in your php.ini file. Register globals is turned off by default as of PHP version 4.2.
Posted: Fri Oct 10, 2003 11:50 pm
by mrvanjohnson
Just change
Code: Select all
<?php
if(isset($id)) // checks if 'id' was part of the link
?>
to
Code: Select all
<?php
$id =$_GET['id'];
if(isset($id)) // checks if 'id' was part of the link
?>
and it should work for you
Posted: Sat Oct 11, 2003 12:01 am
by mrvanjohnson
Another way of doing this would be using the switch function which I think makes for cleaner code. So in you example it would look like this
Code: Select all
<?php
switch ($_GET['id']){
default:
include("mypages/index.php");
break;
case 1:
include("mypages/aboutus.php");
break;
case 2:
echo "Why would you choose this link?";
break;
}
?>
so
http://www.something.com/index.php?id=1 --- would open the About Us page
http://www.something.com/index.php?id=2 --- Would print "Why would you choose this link?"
Anything else should load the index page. Like I said, it's mainly cleaner but I'm not sure if there are any other advantages doing it one way over the other.
Posted: Sat Oct 11, 2003 12:09 am
by nigma
I have done that in the past. The site started out small(about 10 pages), grew real big with many pages (about 30). Image dealing with 30 switches!?!
But using switches could be easier if you only had to deal with a few pages.
Posted: Sat Oct 11, 2003 12:27 am
by gene
mrvanjohnson wrote:Another way of doing this would be using the switch function which I think makes for cleaner code. So in you example it would look like this
Code: Select all
<?php
switch ($_GET['id']){
default:
include("mypages/index.php");
break;
case 1:
include("mypages/aboutus.php");
break;
case 2:
echo "Why would you choose this link?";
break;
}
?>
so
http://www.something.com/index.php?id=1 --- would open the About Us page
http://www.something.com/index.php?id=2 --- Would print "Why would you choose this link?"
Anything else should load the index page. Like I said, it's mainly cleaner but I'm not sure if there are any other advantages doing it one way over the other.
i know this method only....
i want to have a more efficient method..
Posted: Sat Oct 11, 2003 12:43 am
by nigma
Then use the method I me and gen-ik demonstrated =)
Posted: Sat Oct 11, 2003 2:52 am
by itspram
Its bit easy..
say
<?
$id = $row['fieldofid'];
$type = $row['type'];
?>
then
http://www.devnework.net?id=<?echo $id?>&type=<?echo $type?>
hope this is what you want !
PraMs
Posted: Sat Oct 11, 2003 4:50 am
by gene
now i understand, Thanks nigma, Gen-ik, mrvanjohnson and itspram
Posted: Sat Oct 11, 2003 8:17 am
by Gen-ik
Another thing I use to convert all of the variables sent via the url....
Code: Select all
<?php
foreach($_GET as $var -> $value) { ${$var} = $value; }
?>
.... that will simply make the variables available as (for example) $id or $item instead of having to keep using $id=$_GET["id"] or $item=$_GET["item"].... regardless of if register_globals is on or off.