如何在Codeigniter的关联数组中获取访问单个值

时间:2019-09-23 10:44:17

标签: php codeigniter

这是我的控制器和模型。

请在查看页面abc中为我提供帮助,以在表格字段中显示特定的列。

public function abc()
{
    $n=$this->uri->segment(3);
    $svc['id'] =$this->Admin_model->getallserv($n);
    $this->load->view('abc',$svc);
}

public function getallserv($n)
{
    $sql = "select * from services where id=$n";     
    $query = $this->db->query($sql);   
    return $query->result();
}

1 个答案:

答案 0 :(得分:0)

 - Update `$query->result()` to `$query->row_array()`  
 - Then result    will be like `array('column_name' =>
   'value','column_name' =>    'value'.........)`
 - assign to  `$svc` and in view you can access values like $svc->column_name


public function abc()
{
    $n=$this->uri->segment(3);
    $svc =$this->Admin_model->getallserv($n);
    $this->load->view('abc',$svc);
}

public function getallserv($n)
{
  $sql = "select * from services where id=$n";     
  $query = $this->db->query($sql); 
  return  $query->row_array();
}