CodeIgniter Forums
Passing values - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Passing values (/showthread.php?tid=31704)



Passing values - El Forum - 06-29-2010

[eluser]Suhas nazir[/eluser]
Is it possible to pass values to the index function in codeigniter?????


Passing values - El Forum - 06-29-2010

[eluser]mddd[/eluser]
The index function in a controller is only called when no method is provided. For example:
Code:
www.example.com/mycontroller -> this calls the index method in the mycontroller controller
www.example.com/mycontroller/mymethod -> this calls the mymethod method in the mycontroller controller
This means, that you will not get to the index function when there is an argument in the url after the controller name.
If you DO want to call the index method with an argument, you need to use the _remap method. Example:
Code:
function _remap()
{
  $this->index($this->uri->segment(2));
  // if www.example.com/mycontroller/argument is called, segment(2) equals 'argument'
  // so this way, it will result in index() being executed with the argument 'argument'
}

You might want to do some more checking, for instance if www.example.com/mycontroller is called, segment(1) is 'mycontroller' and segment(2) is 'index'. So you should check if the argument is 'index' before handling it. but I hope the idea is clear now.

For more info, check the 'routing' page in the CI manual.


Passing values - El Forum - 06-29-2010

[eluser]pickupman[/eluser]
What are wanting to do because the question has many answers? The one mddd gave you is correct, and can also been done like:
Code:
//In Controller
function index(){
   $this->argument(); //Run index or empty 2nd uri segment to argument method
}

function argument(){
   //Do something here
}

Or perhaps you want to define a default 3rd segment to automatically be set by default
Code:
function index($id = 0){
  //$id = 0 by default
}

//OR
function index(){
   $id = $this->uri->segment(3,0); //$id = 0 by default
}