Page 1 of 1

Parse $_GET From a URL This tutorial is to teach you how to write a function that will return Rate Topic: -----

#1 engale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 3
  • View blog
  • Posts: 594
  • Joined: 30-September 08

Post icon  Posted 10 January 2009 - 02:03 PM

This tutorial is not going to show the best way to do this but the way that I find easy to understand.

The first thing you must understand is that this function is only ment to return the $_GET part of the url. This means the "?" and everything after it in the address bar of the browser.

First off, with every function, you need to start your function, or declare it. We are going to name this function "rtngeturl". You can name it really what ever you like. For me this is an easy to remember name the 'rtn' part of the function name is short hand for return the 'get' part is to remind me that I'm returning the $_GET of the 'url'. See how that works? Easy to remember the name and it tells you what the function will do right in the name. Now that we all understand this lets set up the function.

<?
function rtngeturl() {

}
?>



You will see we have the ( and ) brackets after defining our function. Those are to contain any variables the will contain the values passed to it, but since we are working with a global variable, $_GET, we won't need to fill that with anything, now if you are useing ajax this is a different story but you should understand the ajax enough to get past it. Now then, after the () we have the { and } brackets. These are the container brackets for the code of the function, as in what the function really will do.


Ok we have to find out first how many keys are in the $_GET variable. To do this we simlpy count them. Now because this can, and likely will be different everytime you ask for the function (because if you knew what the url would be everytime you wouldn't need this) we are not going to count these by hand and then tell it instead we are going to have our function count the keys for us useing the count() function already provided to use by PHP. To use this function all you need to know is the variable name of the array, Since $_GET is a global variable set by the URL it's easy to know what the name is. Now all we have to do is tell it to count the keys of $_GET.

<?
function rtngeturl() {
  $c = count($_GET);
}
?>



You will see we used a variable($c) to hold the result of the count($_GET). Reason why we do this is easy to see to, we want to use the value later so we need it stored someplace, and in our case that is the variable $c.

It's great that we have the number of the keys but what are the keys? To find all the names of the keys we can employ another PHP function called array_keys. This function is easy to understand by reading it but I'm going to go a bit into it anyways. The result of the array_keys is another array. The keys to this array start with 0 and go up, setting each key of the array supplyed as the value of the new array key. I hope that make sence, if not you can read more about arrays and array keys at W3schools.

We are going to get all of the keys of $_GET now and assign them to the array we are going to apply name $keys.

<?
function rtngeturl() {
  $c = count($_GET);
  $keys = array_keys($_GET);
}
?>



Now our function knows how many keys $_GET has and the name of those keys. So we can start our set up of the url. We will start by telling the url that it is equal to a subdirectory, in my case I'm working my code fully around the index.php so my subdirectory is "/". Yours may be what ever you like as long as it is the information you KNOW is going before the "?" of your url.

<?
function rtngeturl() {
  $c = count($_GET);
  $keys = array_keys($_GET);
  $url = "/";
}
?>



Now we are going to set up a while loop, this loop is going to be designed with the idea that we are appending the key and its value to the end of the $url variable in the format that would be in the URL. We are going to have our while loop work from 0 to the value of $c. But first we are going to set another variable($i) to 0 so we can add 1 to it at the end of our while loop.

<?
function rtngeturl() {
  $c = count($_GET);
  $keys = array_keys($_GET);
  $url = "/";
  $i = 0;
  while ($i < $c) {
	
	$i++;
  }
}
?>



Now our while loop needs add the "?" but only on the first run, any other run we want it to add the "&" of the url, for this we are going to express and if statment in the format of if it's the first run (if $i equals 0 ) then $url equals the value of $url followed by a "?", else the value of $url equals the value of $url followed by a "&".
<?
function rtngeturl() {
  $c = count($_GET);
  $keys = array_keys($_GET);
  $url = "/";
  $i = 0;
  while ($i < $c) {
	if ($i == 0) { $url = $url."?";}
	else { $url = $url."&";}
	$i++;
  }
}
?>



Ok now we have the loop of our function set up to add the correct variable seperator on each pass of the loop. Now we are going to tell $url on each pass add the next key followed by a "=" with the value of that key to the end of $url.

<?
function rtngeturl() {
  $c = count($_GET);
  $keys = array_keys($_GET);
  $url = "/";
  $i = 0;
  while ($i < $c) {
	if ($i == 0) { $url = $url."?";}
	else { $url = $url."&";}
	$url = $url.$keys[$i]."=".$_GET[$keys[$i]];
	$i++;
  }
}
?>



This function can be use for many situations. The situation that needed this for is a form that I want to return to another from that used $_GET, so they would work together. It surely isn't the prettest way of writing it, infact I rewrote it 3 times while writting this tutorial so it would be easier to follow and still see places where I can change it to improve it or make it simpler to read. You can do that I've given you the start, up to you to make it work the way you want.

PS: $_SERVER['PHP_SELF'] would be something you may want to use to help you in returning a full url. I didn't need a full url returned so I didn't bother writing for the rest of it, but it is verry easy to do from this point if you take the time to do so.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1