Basics of $_GET
This is my First tutorial here hope this help grab people's attention into the forum i wanted to start with something simple and then move on with it.So i will explain some basic uses for the $_GET variable.
First, I know most people will ask, specially if you are new with PHP, whats so special about the $_GET Variable?
Well, theres something really special about this variable, it allows you to get information directly from the url.... yeah thats right theres a way to get fast user input directly from the url no forms or anything, it doesn't necessarily needs to be user input it can also be a script leaving behind some variables for the next script and many other uses you will be able to figure out by yourself after you are done with this tutorial.... the most know use for is used on most CMS and simple page scripts. Still figuring out how does it work? I am pretty sure you will recognize how it works with this example:
Code: Select allhttp://www.martianwabbit.com/page.php?id=home
In the above example you can see something odd about the url the "?id=home" after the file name well thats exactly where your $_GET variables are gotten from. I guess some of you will just think of the thousands of possibilities this opens and, how you can use it to improve your scripts.
But how do i get the information stored on the "?id=home"?
Thats simple first of you need to know that the above its saying that "id" equals "home" just as a variable $x = y;. Now that you know what it means you should be able to get the information easily.
Well to use that information it is recommended that you assign a variable with it so, you could use it directly but you wont be able to edit it along the script also this helps making it easier to call and read.this is how it works:
Code: Select all$id = $_GET['id'];
simple isn't it? now you can call the variable using $id and you will see how it gets whatever text its on the url.
a simple way to see this in action is making a simple script:
Code: Select all<?php
//set $id 's value to whatever is contained on the url.
$id = $_GET['id'];
//echo the variable's value.
echo("$id");
?>
In the above example we set $id with the value that came with url and then we echo 'ed it.
This are the simplified basics behind this variable but you can use this for a multitude of things.Before we go on to the "multitude of things" I wanna show you how to get multiple values from a single url, yep thats right you aren't limited to 1 variable after the ?, you are limited....well you are not limited on the number of values you can get from the url like this.
But how do i get multiple values from the url do I separate them with a comma or what?
well in fact you could separate them with a comma but thats the less practical way the best way is to add additional values to the url like this:
Code: Select allhttp://www.martianwabbit.com/page.php?id=20&page=2
Noticed how i added an additional variable and value there? yeah i simply added a &(meaning "and") and added an additional Name = Value, please notice that there is no additional ? after the &.
ok now i have two variables to play with but how do i manipulate both of them?
well this simple example can explain it better than what i could:
Code: Select all<?php
$id = $_GET['id'];
$page = $_GET['page'];
echo("you are looking for $id's page $page");
?>
see how simple it is? if the file above was named "page.php" and i typed in:
Code: Select allhttp://www.martianwabbit.com/page.php?id=home&page=2
In the browser i would get this:
Code: Select allyou are looking for home's page 2
Easy right? well this is how you add multiple variable names and values through the url.
Now what? What can i do with all this information i just learned? well, I decided to include a little practical example.
Remember that i mentioned that this is commonly used in simple page systems? Well this is how its done:
Imagine you want to have a specific page style to be used to display some articles you could make a new HTML page for each article (which would make you waste time and effort on ) or you could use a dynamic page to grab whatever page is requested from the url and simply append the contents to the page style you wanna use.
If you voted for the second option this is a way of doing it, it might get a little out of the scope of the tutorial but its only to illustrate a use for it:
First, we need to make sure a value is always set, we gonna use the variable $id and $page to specify a page name($id) and a page number($page). to make sure that theres a variable value always not only when its specified on the url (i.e. to display a homepage) we do a simple check !isset (means "is not set") if this is true then a default value is used. this is how it should look like:
Code: Select all<?php
//we set a default value in case no page is being looked for.
if(!isset($_GET['id'])){
//in case no file is being looked up.
$id = "Home";
}else{
//if theres a value.
$id = $_GET['id'];
}
//we set a default value in case no page number is specified.
if(!isset($_GET['page'])){
$page = "1";
}else{
//if theres a value look for it.
$page = $_GET['id'];
}
?>
ok now we are sure the variable is always there next thin we need is the html, our "template" so we make a simple html template mine looks like this:
Code: Select all<html>
<title></title>
</html>
<body>
</body>
Thats enough for it to work. Okay, now its time to add some content. so we add a include into the body.:
Code: Select all<html>
<title></title>
</html>
<body>
<?php include("pages/$id-$page.php"); ?>
</body>
ok that should work perfectly but lets add some additional stuff to make it look nicer and more dynamic... we can add a title dynamically getting it from the page name:
Code: Select all<html>
<title><?php echo("$id"); ?></title>
</html>
<body>
<?php include("pages/$id-$page.php"); ?>
</body>
ok our tile is dynamically changed but this script is still missing some stuff for it to be practical we will add a file check before including the content and an error in case the file doesn't exist:
Code: Select all
<html>
<head>
<title><?php echo("$id"); ?></title>
</head>
<body>
<?php
//make sure file exists before attempting to open it.
if(file_exists("pages/$id-$page.php")){
//if it exists open the file.
include("pages/$id-$page.php");
}else{
//in case file doesn't exist throw a error.
echo("file not found");
}
?>
</body>
now its done you should have this or something quite similar to it:
Code: Select all
<?php
//By Seich(
MWPS:: Martian Wabbit Productions)
//we set a default value in case no page is being looked for.
if(!isset($_GET['id'])){
//in case no file is being looked up.
$id = "Home";
}else{
//if theres a value.
$id = $_GET['id'];
}
//we set a default value in case no page number is specified.
if(!isset($_GET['page'])){
$page = "1";
}else{
//if theres a value look for it.
$page = $_GET['id'];
}
?>
<html>
<!--html part-->
<head>
<!--set title dynamically depending on what file is being read.-->
<title><?php echo("$id"); ?></title>
</head>
<body>
<!--Change body Dynamically depending on file's content-->
<?php
//make sure file exists before attempting to open it.
if(file_exists("pages/$id-$page.php")){
//if it exists open the file.
include("pages/$id-$page.php");
}else{
//in case file doesn't exist throw a error.
echo("file not found");
}
?>
</body>
now you need to create a folder called "pages" in the same directory you have this script , files inside should be ending with a .php and should be named like this "name-number.php" starting from number 1 for it to work with no problems. With this you can now call the file using the a url.
Code: Select allhttp://martianwabbit.com/page.php?id=name&page=2
this would open file : name-2.php in the body.
Okay, there you got it we have gone through all the basics behind the $_GET variable and now you know how special it is.you also know how practical it can be and how much time it can save you (as PHP can in general) and we also went through a practical example which can obviously be expanded as much as you want to make... idk... a full CMS or anything you can dream of...
Hope this helped you understand one important part behind PHP. if by any reason you don't understand something or have a question or simply love it just replay to the thread.