CodeIgniter Forums
How can I pass multiple variables/values through the uri? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How can I pass multiple variables/values through the uri? (/showthread.php?tid=61421)



How can I pass multiple variables/values through the uri? - alexandervj - 04-14-2015

I've been using the uri to pass multiple values to my model with something like...


Code:
<?php echo anchor("main/request_details/$r->submitTime", 'View'); ?>


and using it to make a query in the model with...


Code:
$this->db->where('submitTime', $this->uri->segment(3));


but now I need to pass 2 values through the url. Is there a way to do this or am I going about this the wrong way perhaps? Thanks


RE: How can I pass multiple variables/values through the uri? - CroNiX - 04-14-2015

You can just add a new segment like you are when adding the 3rd.

Parameters are automatically passed to the controller/method as arguments, like
<?php echo anchor("main/request_details/$r->submitTime/anotherParameter", 'View'); ?>

You just need to capture them in the method and would be available like:
PHP Code:
function request_details($submitTime ''$anotherParameter ''//assign the variables being passed, AND set a default value
{
  if ( ! empty(
$submitTime))
  {
    
//submitTime was passed, do something with it
  
}
  
//instead of $submitTime = $this->uri->segment(3);

  
if ( ! empty($anotherParameter))
  {
    
//do something with $anotherParameter
  
}
  
//instead of $anotherParameter = $this->uri->segment(4);


By setting the default values in the method, you can use the controller for:
http://yoursite.com/main/request_details
http://yoursite.com/main/request_details/{submit_time}
http://yoursite.com/main/request_details/{submit_time}/{another_parameter}


RE: How can I pass multiple variables/values through the uri? - alexandervj - 04-14-2015

Thanks! thats exactly what I'm looking for


RE: How can I pass multiple variables/values through the uri? - Muzikant - 04-14-2015

Read Passing URI Segments to your methods.