how to make this?<http://something.com/index.php?id=1&
Moderator: General Moderators
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:
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.
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
?>BTW, I defined the TOPOFPAGE and BOTTOMOFPAGE so that I can easily change which pages to include as the top and bottom.
Last edited by nigma on Fri Oct 10, 2003 8:20 pm, edited 1 time in total.
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...
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
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>- mrvanjohnson
- Forum Contributor
- Posts: 137
- Joined: Wed May 28, 2003 11:38 am
- Location: San Diego, CA
Just change
to
and it should work for you
Code: Select all
<?php
if(isset($id)) // checks if 'id' was part of the link
?>Code: Select all
<?php
$id =$_GET['id'];
if(isset($id)) // checks if 'id' was part of the link
?>- mrvanjohnson
- Forum Contributor
- Posts: 137
- Joined: Wed May 28, 2003 11:38 am
- Location: San Diego, CA
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
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.
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;
}
?>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....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
soCode: 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; } ?>
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 want to have a more efficient method..
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
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
Another thing I use to convert all of the variables sent via the url....
.... 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.
Code: Select all
<?php
foreach($_GET as $var -> $value) { ${$var} = $value; }
?>