我的db中有两个表,一个表中有一个名为utility_id的列,它与另一个表中的cu_id相对应
towns cunits
------- ----------
town_id cu_id
town utility
utility_id
现在我想在cunits $ id页面中显示城镇..我收到错误
遇到PHP错误
严重性:注意
消息:未定义的变量:查询
文件名:templates / view_utility.php
行号:26
我的控制器 - Utilities.php
public function details($id) {
$cudata = $this->utilities_model->getById($id);
$data['utility'] = $cudata['utility'];
$this->load->view('header', $data);
$this->load->view('sidebar-left', $data);
$this->load->view('templates/view_utility', $data); //cunits details page view
$this->load->view('sidebar-right', $data);
$this->load->view('footer', $data);
}
}
我的模型 - Utilities_model.php
function listTowns($id) {
$query = $this->db->get_where('towns', array('utility_id' => $id));
//return $query->result();
$results=$query->result_array();
$data['query']=$query->result_array();
}
我的观点 - templates / view_utility.php
<?php foreach($query as $row): ?>
<li><?php echo $row['town'] ?></li>
<?php endforeach; ?>
我的网址已经过了
本地主机/我的文件夹/公共事业/信息/ $ ID
谢谢
答案 0 :(得分:0)
您没有指定由&#39;查询&#39;到视图,所以你无法达到它。 试试这个:
控制器 - Utilities.php
public function details($id) {
$data = array();
$data['towns'] = $this->utilities_model->listTowns($id);
$this->load->view('header', $data);
$this->load->view('sidebar-left', $data);
$this->load->view('templates/view_utility', $data);
$this->load->view('sidebar-right', $data);
$this->load->view('footer', $data);
}
模型 - Utilities_model.php
public function listTowns($id) {
$query = $this->db->get_where('towns', array('utility_id' => $id));
return $query->result_array();
}
查看 - templates / view_utility.php
<?php foreach($towns as $town): ?>
<li><?php echo $town['name'] ?></li>
<?php endforeach; ?>