alternative to GET

Derek Jones edited this page Jul 5, 2012 · 12 revisions
Clone this wiki locally

By default CodeIgniter's $_GET is unset. What do you do if you want to have the ease of query strings and uri segments at the same time, without having to optionary enable query strings? Try this simple helper function.

Create a simple helper and call it request. Use this function:

function getRequests() 
{ 
    //get the default object 
    $CI =& get_instance(); 
    //declare an array of request and add add basic page info 
    $requestArray = array(); 
    $requests = $CI->uri->segment_array();
    foreach ($requests as $request)
    { 
        $pos = strrpos($request, ':');
        if($pos >0)
        {
            list($key,$value)=explode(':', $request);
            if(!empty($value) || $value='') $requestArray[$key]=$value;
        }
    }
    return $requestArray ; 
}

Your url can be in the notation www.yoursite.com/index.php/class/function/request1:value1/request2:value2

To get the values from your request

$request = getRequests();
echo $request['request1'];
echo $request['request2'];

This function simply loops through the uri segements and picks up only elements with the nation request:value and returns the request array

Category:Contributions::Helpers::PHP