how to make this?<http://something.com/index.php?id=1&

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

gene
Forum Newbie
Posts: 5
Joined: Fri Oct 10, 2003 7:58 pm

how to make this?<http://something.com/index.php?id=1&

Post by gene »

i want to know how ppl can do that
http://something.com/index.php?id=1&c=main
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
Last edited by nigma on Fri Oct 10, 2003 8:20 pm, edited 1 time in total.
gene
Forum Newbie
Posts: 5
Joined: Fri Oct 10, 2003 7:58 pm

Post by gene »

if i want to link the file page1.php
what should i do?
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

gene
Forum Newbie
Posts: 5
Joined: Fri Oct 10, 2003 7:58 pm

Post 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...
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Re: how to make this?<http://something.com/index.php?id=1

Post by Gen-ik »

gene wrote:i want to know how ppl can do that
http://something.com/index.php?id=1&c=main
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!
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post 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
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post 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.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
gene
Forum Newbie
Posts: 5
Joined: Fri Oct 10, 2003 7:58 pm

Post 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..
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Then use the method I me and gen-ik demonstrated =)
itspram
Forum Newbie
Posts: 8
Joined: Tue Sep 16, 2003 6:26 am

Post 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
gene
Forum Newbie
Posts: 5
Joined: Fri Oct 10, 2003 7:58 pm

Post by gene »

now i understand, Thanks nigma, Gen-ik, mrvanjohnson and itspram
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
Post Reply