[英]CodeIgniter join two tables
我正在嘗試使用CodeIgniter將兩個表連接在一起。 我使用CodeIgniter用戶指南尋求幫助。 我遇到一些問題,其中僅顯示一個表的數據,但我不知道為什么。 有人可以幫我嗎?
這是我的代碼:
控制者
function getall(){
$this->load->model('result_model');
$data['query'] =
$this->result_model->result_getall();
$this->load->view('result_view', $data);
}
模型
function result_getall(){
$this->db->select('*');
$this->db->from('tblanswers');
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'left');
$query = $this->db->get();
return $query->result();
}
視圖
<div>
<?php foreach ($query as $row): ?>
//tblanswers
<?php echo $row->answerA;?><br>
<?php echo $row->answerB;?><br>
<?php echo $row->answerC;?><br>
<?php echo $row->comment;?><br>
//credentials
<?php echo $row->name; ?>
<?php endforeach; ?>
</div>
function result_getall(){
$this->db->select('tblanswers.*,credentials.*');
$this->db->from('tblanswers');
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'left');
$query = $this->db->get();
return $query->result();
}
在控制器中嘗試此操作,看看結果如何。此外,如果出現任何錯誤,請告訴我們,並確保表中有數據:)。
控制者
function getall(){
$this->load->model('result_model');
$data['query'] =$this->result_model->result_getall();
print_r($data['query']);
die();
$this->load->view('result_view', $data);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.