Passing values

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

#2
[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.

#3
[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
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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