【问题标题】:How to get the value uri segment in CodeIgniter如何在 CodeIgniter 中获取值 uri 段
【发布时间】:2015-09-14 05:39:47
【问题描述】:

我不了解 CodeIgniter 上的 uri->segment。 作为以下示例,我有一个包含字段的表:

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

如果我打电话给$id = $this->uri->segment(4); 它返回user 字段。

如何改为返回 id_comment 字段?

【问题讨论】:

标签: codeigniter codeigniter-2


【解决方案1】:

如果您的网址是 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

【讨论】:

  • 是的,我知道,但我想调用段 (5) 是值 id_comment 吗??
  • 所以你必须在你的 URL 中传递它
  • 怎么样?如果你能帮助我?制作脚本。删除基于 id_comment 的 cmets 的脚本
【解决方案2】:

例如,您的网址看起来很像: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() ..

从您的网址访问参数
public function edit($id, $name, $age)
{
   echo $id;
}

【讨论】:

    【解决方案3】:

    //控制

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

    //查看

    【讨论】:

      猜你喜欢
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 2012-03-02
      • 2014-12-18
      • 1970-01-01
      • 2018-06-07
      • 1970-01-01
      • 2015-11-04
      相关资源
      最近更新 更多