Codeigniter get parameters from url

2019-09-19 05:50发布

I have a url "http://localhost/codeigniter/index.php/manual_export/". I need to get last segment from the url which is suppose to be a id. So for example "http://localhost/codeigniter/index.php/manual_export/2". I need to get the last segment which is "2".

I tired to use following code:

$id = end($this->uri->segment_array()); 

This works when I don't add "2" to the url and gives me "manual_export". However when I pass the id to the url I get an error "The page you requested was not found.". I think this is to do with routing. How can I fix this error.

2条回答
女痞
2楼-- · 2019-09-19 06:36

the other way to do it is by defining a route, it will then be converted to a param

so for example if your controller is called manual_export and the method is getrecord

in the file application/routes.php

$route['manual_export/(:any)'] = "manual_export/getrecord/$1";

in your controller manual_export

function getrecord($id){ // etc etc }
查看更多
祖国的老花朵
3楼-- · 2019-09-19 06:40

You should use:

$this->uri->segment(n);//in your case n == 2 count starts just after index.php

Docs.

查看更多
登录 后发表回答