如何从视图页面中的站点URL获取ID

时间:2017-05-03 19:19:14

标签: php html database codeigniter adminlte

http://localhost/cisbeadmin/index.php/conuser/edituser/.$row->$id_pegawai

如何从CodeIgniter中的网站网址获取ID? 这是我在视图页面中的代码:

add

2 个答案:

答案 0 :(得分:0)

首先,您不需要直接进入视野。你应该用控制器的方法来获取它。在您的情况下,方法名称是 edituser

以下是从url获取id的方法:

// Make sure your segment possition is correct and place few lines of code in starting of your method edituser()
$id = $this->uri->segment(3)) != '' && is_numeric($this->uri->segment(3)) && $this->uri->segment(3) > 0 ? $this->uri->segment(3) : false;

// After getting $id check if it is false or having value in it
if($id == false){
// If URL does not having id then it should show 404 error which is done by below line
   show_404();
}

// Pass $id value for view file same as we pass other values.

希望我详细解释。如果您对此有任何疑问,请与我联系。

答案 1 :(得分:0)

  

用此

代替代码
<a href="<?php echo site_url('conuser/edituser/'.$row->$id_pegawai); ?>  "> <i class="fa fa-edit"></i></a>

并且您可以按照以下代码在视图中获取ID

$id_pegawai = $this->uri->segment(2);