Jump to content

This might be a really stupid question...


Klance

Recommended Posts

I've noticed how most PHP sites have all their pages comming off of one. Like /index.php?login or /index.php?register

 

How is this done.. I know this is a very newb question but could somone tell me or link me to a tutorial where it explains this, i've been reading a lot of tutorials recently and none have explained this.

 

Thanks in advance.

Link to comment
Share on other sites

Put this in your index.php file:

 

<?php if(!empty($_GET['id']) && file_exists("./$_GET[id].php")){ include("./$_GET[id].php"); } else { include("./inc/main.php"); } ?>

 

Then, in your index page put a link like this:

 

<a href="?id=hello">Hello Page</a>

 

Now create a new .php page called hello.php.

 

Then once you have done that run index.php and click the hyperlink. Hey presto, you included a page.

 

 

(You also need to creat main.php so that if a link is broken main.php loads by default. For example main.php would be your homepage.)

Link to comment
Share on other sites

Thanks, now how would I build off of that.. Say I have /index.php?id=GH and on GH.php I want to have it open another page, say PS2.php so that it would be /index.php?id=GH?id=PS2

 

I tried just adding the code you gave me that I added to index.php to GH.php and it doesn't add onto /index.php?id=GH. It starts a new page thats /index.php?id=PS2

Link to comment
Share on other sites

I put this code in GH.php

<php

if(!empty($_GET['id2']) && file_exists("./$_GET[id2].php")){ include("./$_GET[id]2.php"); }

?>

then this link

<a href="?id2=PS2">PS2</a>

 

and it just opens /index?id2=PS2

when i want it to open /index?id=GH?id2=PS2

 

what am i doing wrong?

Link to comment
Share on other sites

<a href="?id2=PS2">PS2</a>

 

By doing this you are making it look for one value instead of the tree value you want...

 

instead do this

 

<a href="index.php?id='.$PHP_SELF.'?id2=PS2">PS2</a>

 

I haven't tested this because I don't have time to but that should make the id value to the filename of the page it is currently running through.

 

Hope this does what you want it to. :)

Link to comment
Share on other sites

<a href="?id2=PS2">PS2</a>

 

By doing this you are making it look for one value instead of the tree value you want...

 

instead do this

 

<a href="index.php?id='.$PHP_SELF.'?id2=PS2">PS2</a>

 

I haven't tested this because I don't have time to but that should make the id value to the filename of the page it is currently running through.

 

Hope this does what you want it to. :)

 

that jsut does this: index.php?id='.$PHP_SELF.'?id2=PS2

Link to comment
Share on other sites

<a href="?id2=PS2">PS2</a>

 

By doing this you are making it look for one value instead of the tree value you want...

 

instead do this

 

<a href="index.php?id='.$PHP_SELF.'?id2=PS2">PS2</a>

 

I haven't tested this because I don't have time to but that should make the id value to the filename of the page it is currently running through.

 

Hope this does what you want it to. :)

 

that jsut does this: index.php?id='.$PHP_SELF.'?id2=PS2

 

Sorry lol forgot something important...

 

<a href="index.php?id=<?php echo $_SERVER['PHP_SELF']; ?>?id2=PS2">PS2

 

If the script doesn't like the PHP_SELF variable then you would have to do it manually by making the href value be "index.php?id=gh?id2=PS2"

 

But laffin does bring up an interesting point...the site you linked to is using a database (DB) to store the data and calculate things. If you are trying to do something like them you will need to work with a database.

Link to comment
Share on other sites

I've noticed how most PHP sites have all their pages comming off of one. Like /index.php?login or /index.php?register

 

How is this done.. I know this is a very newb question but could somone tell me or link me to a tutorial where it explains this, i've been reading a lot of tutorials recently and none have explained this.

 

Thanks in advance.

 

The 'login' in /index.php?login and the 'register' in /index.php?register are referring to variables that are submitted to the script via the URL.  They are not different pages, but just variables that are sent to the script at index.php for processing.  So you might see /index.php?login=true&username=pauleth.  When the script receives this, it defines two variables with those values.  Here is an example:

 

$login = $_GET['login'];  // get the value stored in login in the URL

$user = $_GET['username'];  // get the value stored in username in the URL

 

Now we have a variable called $login that contains the value 'true', and a variable called $user that contains the value 'pauleth'.

 

You may have entire portions of your script that are only executed if a variable holds a particular value.  Hence, someone who is actually logged into a site may see something quite different than an unregistered guest, such as a welcome message like: "Welcome back, pauleth!"  I hope that helps...

Link to comment
Share on other sites

<a href="?id2=PS2">PS2</a>

 

By doing this you are making it look for one value instead of the tree value you want...

 

instead do this

 

<a href="index.php?id='.$PHP_SELF.'?id2=PS2">PS2</a>

 

I haven't tested this because I don't have time to but that should make the id value to the filename of the page it is currently running through.

 

Hope this does what you want it to. :)

 

that jsut does this: index.php?id='.$PHP_SELF.'?id2=PS2

 

Sorry lol forgot something important...

 

<a href="index.php?id=<?php echo $_SERVER['PHP_SELF']; ?>?id2=PS2">PS2

 

If the script doesn't like the PHP_SELF variable then you would have to do it manually by making the href value be "index.php?id=gh?id2=PS2"

 

But laffin does bring up an interesting point...the site you linked to is using a database (DB) to store the data and calculate things. If you are trying to do something like them you will need to work with a database.

 

I know that once you get all the way down to the scores it gets those scores from a database. I'm not worried about getting the scores from a db right now, i'm just worried about getting the tree to work out, getting from the game to the platform to the difficulty...

 

That code still does not work btw, i don't know whats wrong with it.

Link to comment
Share on other sites

I know that once you get all the way down to the scores it gets those scores from a database. I'm not worried about getting the scores from a db right now, i'm just worried about getting the tree to work out, getting from the game to the platform to the difficulty...

 

Thats the question u shud have been focusing on, instead of asking generic questions.

Get as specific as possible

 

U wud want to use an array/mysql for the input validations.

a very simple structure wud be

$games = array (
  'Game1' => array ( 
        'Consoles' => array ('Ps2','XBOX','PSP'),
        'Diff' => array ('Easy','Medium','Hard')
   ),
  'Game2' => array ( 
        'Diff' => array ('Easy','Medium','Hard')
  )
)

 

u do your verifications either by walking the array, against the $_GET values

 

if(isset($_GET['game']))
{
   if(array_key_exists($_GET['game'],$games))   // we are in 1st level checking
   {
       $game_array=$games[$_GET['game']];
       // 2nd lvl checking - 1st check for consoles
       if(isset($game_array['Consoles'] && isset($_GET['console']))
       {
          if(in_array($_GET['console'],$game_array['Consoles']))
          { // we got a valid console
                $console=$_GET['console'];
          }
       }
       if(isset($game_array['Diff'] && isset($_GET['diff']))
       {
          if(in_array($_GET['diff'],$game_array['Diff']))
          { // we got a valid difficulty
                $diff=$_GET['diff'];
          }
       }
    }
}

 

so now u can build a url like

http://my.site.com?game=xxx&console=xbox&diff=hard

 

this is a simple example, u will need more checking for console setting if it exists prior to setting the diff setting

Link to comment
Share on other sites

<?php
$page = $_GET['page'];
if (isset($error)){
echo $error;
echo '<p>';
}
	if ($page == NULL){
		include ('pages/home.php');
	}
		elseif (file_exists('pages/' . $page . '.php')){
			include ('pages/' . $page . '.php');
	}
			else include ('pages/error404.php');
echo "<br />";
?>

Link to comment
Share on other sites

I know that once you get all the way down to the scores it gets those scores from a database. I'm not worried about getting the scores from a db right now, i'm just worried about getting the tree to work out, getting from the game to the platform to the difficulty...

 

Thats the question u shud have been focusing on, instead of asking generic questions.

Get as specific as possible

 

U wud want to use an array/mysql for the input validations.

a very simple structure wud be

$games = array (
  'Game1' => array ( 
        'Consoles' => array ('Ps2','XBOX','PSP'),
        'Diff' => array ('Easy','Medium','Hard')
   ),
  'Game2' => array ( 
        'Diff' => array ('Easy','Medium','Hard')
  )
)

 

u do your verifications either by walking the array, against the $_GET values

 

if(isset($_GET['game']))
{
   if(array_key_exists($_GET['game'],$games))   // we are in 1st level checking
   {
       $game_array=$games[$_GET['game']];
       // 2nd lvl checking - 1st check for consoles
       if(isset($game_array['Consoles'] && isset($_GET['console']))
       {
          if(in_array($_GET['console'],$game_array['Consoles']))
          { // we got a valid console
                $console=$_GET['console'];
          }
       }
       if(isset($game_array['Diff'] && isset($_GET['diff']))
       {
          if(in_array($_GET['diff'],$game_array['Diff']))
          { // we got a valid difficulty
                $diff=$_GET['diff'];
          }
       }
    }
}

 

so now u can build a url like

http://my.site.com?game=xxx&console=xbox&diff=hard

 

this is a simple example, u will need more checking for console setting if it exists prior to setting the diff setting

 

Thank you for taking the time out to write this for me. So how would I put this code to use? where would the links go etc? sorry if i'm annoying you but i'm new to PHP.

Link to comment
Share on other sites

its all in the checking....

 

first check is Games.

if u dun have a Game, than display the games list

 

if(!isset($game))
{
   foreach($games as $key => $game)
      echo "<A HREF=?game=$key>$key</A>&NBSP;";
} else if(isset($game_array['Consoles']) && !isset($console) { // display Console list
   foreach($game_array['Consoles'] as $console)
      echo "<A HREF=?game=$game&console=$console>$console</A>&NBSP;";
} else if(!isset($diff)) {  // Display Difficulty levels
   if(isset($console)) $cd="&console=$console"; // Do we have console set? if so set up parameter
   else $cd=''; // otherwise leave parameter empty
   foreach($game_array['Diff'] as $diff)
      echo "<A HREF=?game=$game$cd&diff=$diff>$diff</A>&NBSP;";
} else { We have our necessary parameters display the info
   // Whatever info echo out
}

note: on the 2nd else if, since this parameter is optional we do 2 checks, one if it's in the game_array, and the other if the parameter has been set.

 

also u want to set $game from the previous post

if(array_key_exists($_GET['game'],$games))   // we are in 1st level checking
   {
       $game=$_GET['game'];
       $game_array=$games[$_GET['game']];

 

     

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.