访问codeigniter中的数组元素"id"不起作用。找不到失败的可能原因。如果有人能展示正确的方法,那将是有帮助的,
我的视图
<form name ="userinput" onsubmit="return test()" action="<?php echo base_url(); ?>index.php/patient/saveNewExaminationChart/<?php echo $pd['id']; ?>" method="post">
<?php echo $pd['id']; ?>
对于这一部分,我想获得控制器传递的id号来查看
我的控制器
public function editPeriodicalDetails(){
$pid = $this->uri->segment(3);
$this->load->model('patient_model');
$patient['pd'] = $this->patient_model->getPeriodicalChart($pid);
$this->load->helper('url');
$this->load->view('Header');
$this->load->view('Patient/editPeriodicalExam',$patient);
$this->load->view('Footer');
}
我的型号
public function getPeriodicalChart($id){
$this->load->database();
$this->db->select('*');
$this->db->from('periodical_examination a');
$this->db->join('patient p','p.id = a.pid');
$this->db->where('a.pid',$id);
$query = $this->db->get();
return $query->result();
}
在模型中
return $query->result_array();
在控制器中
$result= $this->patient_model->getPeriodicalChart($pid);
$patient = $result[0]['pd']; # zero indexed array pointer
result((是递归的,因为它返回一个std类对象,而as result_array((只返回一个纯数组,所以result_arra((将是关于性能的选择。在速度上差别很小。任何有php基础知识的人都会发现很容易阅读代码
而不是这条线
$query = $this->db->get();
return $query->result();
(只是用这个它真的很容易理解(
return $this->db->get()->result_array();
它应该是一个对象。试试这个
<?php echo $pd->id; ?>
如果有不止一个结果集,那么试试这个
<?php echo $pd[0]->id; ?>
$query->result();
是Object,而不是数组。。要返回数组,您应该使用
$query->result_array();
在这里,您在Model中使用了return $query->result();
,并通过Controller将所有表发送到视图。干得好。一切都很好。现在,您只需在视图页面中使用以下代码来echo
和id
。
视图(编辑定期检查(
<?php
foreach($pd as $item){
echo $item['id'];
}
?>