如何在CodeIgniter中获取值uri段

sc4hvdpw  于 2023-03-27  发布在  其他
关注(0)|答案(4)|浏览(135)

我不理解CodeIgniter上的uri->segment。如以下示例,我有一个表,其中包含字段:

| Id_comment | id_alat     | user         |
|:-----------|------------:|:------------:|
|     1      |    45       | irwan        |

如果我调用$id = $this->uri->segment(4);,它返回user字段。
如何返回id_comment字段?

2wnc66cl

2wnc66cl1#

如果你的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
yr9zkbsy

yr9zkbsy2#

例如,您的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/$1/$2/$3;

最后,在控制器中,您可以通过uri->segment()..从url访问参数。

public function edit($id, $name, $age)
{
   echo $id;
}
hkmswyz6

hkmswyz63#

//控制

function deletecom() {
    $u = $this->uri->segment(4);
    $this->mcrud->deletecomment($u);
    redirect('instrument/instrument');
}

//viewing

s6fujrry

s6fujrry4#

自CodeIgniter 4以来,这略有变化:

$uri = service('uri');
$sLast = $uri->getSegment(4, 'any default value');

相关问题