我有两个表患者(姓名,id_paz)和访问(id_visita,id_paz,日期),1名患者可能有更多访问。如下所示,我检查错误:"未定义变量:查询"和#34;为foreach()"
提供的参数无效型号:
function result_getall(){
$this->db->select('*');
$this->db->from('paziente');
$this->db->join('visita', 'paziente.id_paz = visita.id_visita', 'left');
$query = $this->db->get();
return $query->result($sql);
}
查看:
<?php foreach ($query as $row): ?>
<?php echo $row->id_paz?><br>
<?php echo $row->nome?><br>
<?php echo $row->cognome?><br>
<?php echo $row->id_visita ?>
<?php endforeach; ?>
控制器:
function getall(){
$this->load->model('visita_model');
$data['query'] =$this->visita_model->result_getall();
print_r($data['query']);
die();
$this->load->view('aggiungi_view', $data);
}
答案 0 :(得分:1)
也许你的阵列没有设定。
尝试使用print_r
检查它或添加如下所示的if语句:
<?php
if(isset($query)){
foreach ($query as $row): ?>
<?php echo $row->id_paz?><br>
<?php echo $row->nome?><br>
<?php echo $row->cognome?><br>
<?php echo $row->id_visita ?>
<?php endforeach;
}
?>
并在你的控制器中更改:
return $query->result($sql);
到此:
$return = array();
foreach ($query->result() as $row)
array_push($return, $row);
return $return;