Jump to content

Multiple GET Variables


misterivanovich
 Share

Recommended Posts

I posed this question in my other thread, but I didn't get as detailed of an answer as I had hoped. I did get some code that I could include in order to create a "forum style" URL system using the switch protocol and having pages such as post.php, viewforum.php and viewthread.php... But I don't know what to include in those .php files, nor do I know what using the include protocol actually DOES. All I know is that I need to use it.So, I pose this question again and hope for more of an explaination rather than code to do what I want. How do I use multiple GET variables in my URL to display a multitude of different pages while remaining on the index.php page? My example is of this forum.No matter what you click or where you visit, you still see the index.php. However, after that you see ?act=post&do=new_post, etc. All of those get variables display completely different pages. I can guess that it all has to do with what you've told me - ie, the switch and include protocols. However, just telling me I need different .php files to "include" when I have certain GET variables doesn't quite put it in perspective for me. =[ What am I to include on said variables?Also, I assume it's best to have them in order from least descriptive to most specific, correct? For instance, for my school website I would want to go from the splash page ((index.php)) to the teacher's main page ((index.php?t=deramo)) to the classes they teach ((index.php?t=deramo&c=cbip))...I guess my biggest question is how to use that include thing with the other .php files and what information // coding those .php files need to have. Actually, it's my only question.

Link to comment
Share on other sites

THis is pretty simple.You could use if file exists, or switch.I'll take an example of switch:

<?phpswitch($_GET['p']) { // index.php?p=case '': include 'main.php'; break; // If p is empty - ?p=case 'something': include 'something.php'; break; // ?p=somethingcase 'hello': include 'something_else.php'; break; // ?p=hellodefault: include '404_error.php'; break;}?>

And then if you want ?p=something&q=something_else, you could i.e in something.php write a new switch just get another letter or something, if you understand.

Link to comment
Share on other sites

The first thing you want to do on the page is get all of the variables that may be passed to the page, with defaults. That would look something like this:

<?phpif (isset($_GET['mode']))  $mode = $_GET['mode'];else  $mode = "default";  //default//this is shorthand for the above statement, they will do the exact same thing$mode = (isset($_GET['mode']) ? $_GET['mode'] : "default");//get the other variables the same way, might as well save space and use the shorthand$t = (isset($_GET['t']) ? $_GET['t'] : ""); // $t defaults to ""$page = (isset($_GET['page']) ? intval($_GET['page']) : 1); // $page defaults to 1?>

I was just using example variables there, the point is for you to get everything you might use at the top of the page. Having access to everything will let you decide what the script needs to do for the given inputs. After getting the variables you can test anything that needs to be tested and output error messages if the validation failed. For example, if $page were -1 you might want to show an error. Generally what you want to do is have a switch statement or a series of if statements that will decide what needs to happen based on the variables from $_GET. This could look as simple as this:

switch($mode){  case "user":	include("user.php");	break;  case "file":	include("file.php");	break;  case "thread":  case "view":  case "post":  case "forum":	include ("forum.php");	break;  default:	include("home.php");	break;}

Or you can make it as complex as you need to in order to accomplish whatever you are trying to accomplish.As far as the contents of include files go, it's no different then anything else. You can think of PHP as replacing the call to include the file with the contents of the file itself. So if you have this code:

<?php$var1 = "Hello world";echo $var1;$var2 = 15;$var3 = 27;$var4 = 42;$result = $var2 * $var3 / $var4;echo $result;?>

You could break the code up into an include file like this:

<?php$var1 = "Hello world";echo $var1;include ("include.php");?>

include.php:

<?php$var2 = 15;$var3 = 27;$var4 = 42;$result = $var2 * $var3 / $var4;echo $result;?>

And the result would be the same. It is as if PHP just inserts the code in the included file where the include statement is and executes it normally. The code in the included file has access to all of the same variables and anything else that the other code does, it just happens to be stored in a physically separate file.

Link to comment
Share on other sites

**Bump + Question**Would it be terrible coding form to put my IF statements for the classes inside of the IF statements for the teachers? Like this.

$t = array('deramo', 'schoenleb', 'welker', 'king', 'meadows');$c = array('cbip');if (isset($_GET['t'])){	if (in_array($_GET['t'], $t))	{		$teacher = $_GET['t'];	}	else	{		$teacher = 'error';	}	if (isset($_GET['c'))	{		etc...	}	else	{		$class = '';	}}else{	$teacher = '';	$class = '';}

I haven't even gone through and added the full list of classes, but still. This is just the general idea. I realize that if someone were to try and do index.php?c=cbip it would just let them go straight to the splash page, and that's not right... But does that really matter? Should I have a check after that that says if (isset($_GET['c']) && !isset($_GET['t']) then have a header to go back to index.php with no extension?Just curious as to what the "proper" way to code that would be.

Link to comment
Share on other sites

It's fine to use a structure like that. You would only use a switch statement if you were testing one variable for several values. So you could replace this with a switch:

if ($var == 1){  ...}elseif ($var == 2){  ...}elseif ($var == 3){  ...}elseif ($var == 4){  ...}else{  ...}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...