Welcome Guest, Not a member yet? Register   Sign In
Passing Parameters to Controllers
#1

[eluser]cyberbuff[/eluser]
I have a controller named latest_entries. I have a pagination too. Now i want to pass a variable (page number) to the controller. that is http://mysite/latest_entries/2 where 2 is the page number. I tried to pass the variable to the index() function but didn't work. Someone with a clue?
Regards
Abhisek
#2

[eluser]Colin Williams[/eluser]
Since you can't seem to extrapolate from the User Guide, I'll try to explain it directly.

In CodeIgniter, you can view a url like this: http://www.example.com/controller/function/category/id

controller: This is the controller file and class that is invoked
function: This is the member function of the controller class that is called
category: This is passed as the first parameter to function
id: This is passed as the second parameter to function

Any more segments in the URI beyond these need to be fetched with one of the URI class methods ($this->uri->segment(), $this->uri->rsegment(), etc).
#3

[eluser]Sumon[/eluser]
Give it a try:
Add this line in your route.php
Code:
$route['latest_entries/(:any)'] = "latest_entries/index";
now check http://mysite/latest_entries/2
and pick up URI segments using
Code:
$first = $this->uri->segment(3);
$second =$this->uri->segment(2);
#4

[eluser]cyberbuff[/eluser]
thanks sumon and williams.
@sumon, dhonyobad, i almost got there by editing routes.php but it's showing me the same page again and again. I mean /latest_entries/10 and /latest_entries/20 is the same page. Sad also i didn't get what you meant by "picking up URI segments"
regards
Abhisek
#5

[eluser]Michael Wales[/eluser]
Code:
$route['latest_entries/(\d+)'] = 'latest_entries/index/$1';

Code:
function index($offset) {
  echo $offset;
}
#6

[eluser]Sumon[/eluser]
@Abhisek i m really surprised to see dhonyobad. ha ha Smile however, i know i give you the solution is not 100% perfect. i actually don't know what's the way to prevent all functions to hit directly into index function.

@michael Wales: Thanks for write perfect one. actually i have seen it in a post here, but don't find that.

Ah!!! Currently i found that in CI user guide . What a foolish i am that i am not enough passionate to study URI Routing in User Guide.
#7

[eluser]cyberbuff[/eluser]
thanks Michael Wales. It's working perfectly. and thanks to sumon too. I am bengalee guy living in WB. Smile




Theme © iAndrew 2016 - Forum software by © MyBB