Klance Posted March 20, 2008 Share Posted March 20, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/ Share on other sites More sharing options...
Schlo_50 Posted March 20, 2008 Share Posted March 20, 2008 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.) Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-496757 Share on other sites More sharing options...
Klance Posted March 20, 2008 Author Share Posted March 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-496863 Share on other sites More sharing options...
craygo Posted March 20, 2008 Share Posted March 20, 2008 use a different variable index.php will be looking for id have gh.php look for id2 so index.php?id=gh&id2=PS2 so now index will load up gh and gh will load up PS2 Ray Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-496866 Share on other sites More sharing options...
Klance Posted March 20, 2008 Author Share Posted March 20, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-496874 Share on other sites More sharing options...
Klance Posted March 20, 2008 Author Share Posted March 20, 2008 is this even possible? Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-496974 Share on other sites More sharing options...
rhodesa Posted March 20, 2008 Share Posted March 20, 2008 What is your reason for keeping GH in the URL and not just using index.php?id=PS2 Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-496976 Share on other sites More sharing options...
laffin Posted March 20, 2008 Share Posted March 20, 2008 first u shud think how parameters shud be passed, and verified. do they all need to exist, or are u building them from previous sessions. than test those parameters and validate them. Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-496988 Share on other sites More sharing options...
Klance Posted March 20, 2008 Author Share Posted March 20, 2008 What is your reason for keeping GH in the URL and not just using index.php?id=PS2 Because people will be viewing things based on the links they choose, theirs GH, GH2 etc... and they all have PS2 under those. I'm trying to simulate what scorehero has here: http://www.scorehero.com/top_scores.php Can anyone help me with that Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-496992 Share on other sites More sharing options...
RottNKorpse Posted March 20, 2008 Share Posted March 20, 2008 <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. Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-497018 Share on other sites More sharing options...
Klance Posted March 20, 2008 Author Share Posted March 20, 2008 <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 Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-497054 Share on other sites More sharing options...
laffin Posted March 20, 2008 Share Posted March 20, 2008 To me that page looks like it relies more on a DB for information than it does on php calculations. so plan and organize yer data layout first. Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-497122 Share on other sites More sharing options...
Klance Posted March 20, 2008 Author Share Posted March 20, 2008 To me that page looks like it relies more on a DB for information than it does on php calculations. so plan and organize yer data layout first. I don't get what you mean. Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-497184 Share on other sites More sharing options...
RottNKorpse Posted March 20, 2008 Share Posted March 20, 2008 <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. Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-497200 Share on other sites More sharing options...
pauleth Posted March 20, 2008 Share Posted March 20, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-497212 Share on other sites More sharing options...
Klance Posted March 21, 2008 Author Share Posted March 21, 2008 <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. Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-497257 Share on other sites More sharing options...
laffin Posted March 21, 2008 Share Posted March 21, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-497283 Share on other sites More sharing options...
Crew-Portal Posted March 21, 2008 Share Posted March 21, 2008 <?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 />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-497284 Share on other sites More sharing options...
Klance Posted March 21, 2008 Author Share Posted March 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-497303 Share on other sites More sharing options...
laffin Posted March 21, 2008 Share Posted March 21, 2008 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']]; Quote Link to comment https://forums.phpfreaks.com/topic/97081-this-might-be-a-really-stupid-question/#findComment-497316 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.