...

World Wide Web Server 2012-07-04 16:06:31 -07:00
parent e5a1bb58bc
commit be0b5807be

@ -0,0 +1,97 @@
Hi I made a little improvement to the helper request builded in "Alternative to GET" and made a way to put multiples variables in a same GET segment. The url notation would be like this
www.yoursite.com/index.php/class/function/value1/variable2:value2::variable3:value3::variableN:valueN
As yo see I used "::" to spread variables in the same uri segment, it even works to put a non predictable count of elements; i used it for example with the pagination library. This is the code of the new Request helper:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/***
* request_helper.php by EliuX(eliux.softwares@gmail.com)
**/
//Receive an array like key:value
function getRequests($requests)
{
//get the default object
$CI =& get_instance();
//declare an array of request and add basic page info
$requestArray = array();
if($requests==array("")) //If there is nothing specified, finish it!
return $requests;
foreach ($requests as $request)
{
if(strrpos($request, ':') >0)
{
list($key,$value)=explode(':', $request);
if(!empty($value) || $value='') $requestArray[$key]=$value;
}else{
$requestArray[]="$request";
}
}
return $requestArray;
}
function multiRequests($params)
{
//get de root object
$CI = & get_instance();
$duplesArray = array();
if(strrpos($params, '::') > 0) //If there is the variables separator, the spread it!
{
$duplesArray = explode('::',$params);
return getRequests($duplesArray); //Each dupla will be spread too!
}else{ //Si no hay mas de una variable
return getRequests(array($params));
}
}
?>
You have to call the funcion multiRequests and in its param the variable of the controller function you know has the multiples variables ":" separated. This is an example:
<?
//Test controller
class Test extends Controller
{
function Test()
{
parent::Controller();
$this->load->helper("request");
}
//Testing the multiRequests function
function testing($var1,$variables)
{
//I take the multiples variables specified in this controller variable segment
$params = multiRequests($variables);
echo "The variable 1 is $var1";
foreach($params as $param=>$value)
echo "<br/>Variable $param is ".$value;
}
}
So an url like http://localhost/test/testing/value1/variable2:value2::variable3:value3::variable4:value4 will print:
The variable 1 is value1
Variable variable2 is value2
Variable variable3 is value3
Variable variable4 is value4
An request like http://localhost/cubel/test/testing/value1/variable2:value2 will print:
The variable 1 is value1
Variable variable2 is value2
and an url like http://localhost/cubel/test/testing/value1/variable2 will print:
The variable 1 is value1
Variable 0 is variable2
Look how at this final example it formated the sole variable in a simple array with initial index in 0.
I hope you'll find it usefull.
------------------------------
Let me know some doubts or suggestions at eliux.softwares@gmail.com. Good coding to yall!