如何在 CodeIgniter 中获取值 uri 段
How to get the value uri segment in CodeIgniter
我不了解 CodeIgniter 上的 uri->segment
。
如以下示例,我有一个 table 字段:
| Id_comment | id_alat | user |
|:-----------|------------:|:------------:|
| 1 | 45 | irwan |
如果我打电话给$id = $this->uri->segment(4);
它 return 是 user
字段。
如何 return id_comment
字段?
如果你的URL是这个instrumentdev/instrument/instrument/detail/CT-BSC-001
然后就可以通过下面的方法获取数据了
$url1 = base_url(); // Site URL
$url2 = $this->uri->segment(1); // Controller - instrument
$url3 = $this->uri->segment(2); // Method - instrument
$url4 = $this->uri->segment(3); // detail
$url5 = $this->uri->segment(4); // CT-BSC-001
//对照
function deletecom() {
$u = $this->uri->segment(4);
$this->mcrud->deletecomment($u);
redirect('instrument/instrument');
}
//查看
例如,您的 url 看起来很像:http://localhost/yourProject/users/edit/1/john/33
所以你有五个参数:
1. users - the controller
2. edit - the function
3. 1 - the id of user
4. john - the name of user
5. 33 - the age of user
在您的 route.php
文件中,您应该创建以下路由:
$route['users/edit/(:num)/(:any)/(:num)'] = 'users/edit///;
最后,在您的控制器中,您可以通过 uri->segment()
或 ..
从 url 访问参数
public function edit($id, $name, $age)
{
echo $id;
}
我不了解 CodeIgniter 上的 uri->segment
。
如以下示例,我有一个 table 字段:
| Id_comment | id_alat | user |
|:-----------|------------:|:------------:|
| 1 | 45 | irwan |
如果我打电话给$id = $this->uri->segment(4);
它 return 是 user
字段。
如何 return id_comment
字段?
如果你的URL是这个instrumentdev/instrument/instrument/detail/CT-BSC-001
然后就可以通过下面的方法获取数据了
$url1 = base_url(); // Site URL
$url2 = $this->uri->segment(1); // Controller - instrument
$url3 = $this->uri->segment(2); // Method - instrument
$url4 = $this->uri->segment(3); // detail
$url5 = $this->uri->segment(4); // CT-BSC-001
//对照
function deletecom() {
$u = $this->uri->segment(4);
$this->mcrud->deletecomment($u);
redirect('instrument/instrument');
}
//查看
例如,您的 url 看起来很像:http://localhost/yourProject/users/edit/1/john/33
所以你有五个参数:
1. users - the controller
2. edit - the function
3. 1 - the id of user
4. john - the name of user
5. 33 - the age of user
在您的 route.php
文件中,您应该创建以下路由:
$route['users/edit/(:num)/(:any)/(:num)'] = 'users/edit///;
最后,在您的控制器中,您可以通过 uri->segment()
或 ..
public function edit($id, $name, $age)
{
echo $id;
}