• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing multiple parameters in a single URL Segment

#1
[eluser]CroNiX[/eluser]
On occasion, I've wanted to pass multiple parameters in a single url segment, much like a query string. This is similar to $uri->uri_to_assoc() except it uses a single segment.

For instance, take the following URL structure:
http://yoursite.com/controller/method/ke...;key3=val3
Code:
$values = $this->uri->segment_to_assoc(3);  //or $this->uri->rsegment_to_assoc(3); for routed segments
print_r($values);

//outputs:
array (
  [key1] => val1,
  [key2] => val2,
  [key3] => val3
)

I just believe this is more semantic (in certain scenarios) than:
http://yoursite.com/controller/method/ke.../key3/val3
Code:
$values = $this->uri->uri_to_assoc(3);
print_r($values);

//outputs:
array (
  [key1] => val1,
  [key2] => val2,
  [key3] => val3
)

Read the notes. You have to add 2 characters to your $config['permitted_uri_chars'] (;=).

I created this for a pagination library I'm using so I could:
www.yoursite.com/controller/method/page=1;perpage=25;filter1=url;filter2=company_name;order=desc
Code:
$pagination_config = $this->uri->segment_to_assoc(3);
$pagination = $this->load->library('pag', $pagination_config);

Anyway, here it is on Github if it would be of use to you. If you find anything that needs fixing, please let me know or initiate a pull request!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.