我正在查看CodeIgniter文档并且只找到一种获取整个表的方法,我正在寻找一种方法来获取UserID
和CourseTitle
列来自不同但相关的表格的一行。
我有这张表:
USER_TABLE
UserID | CourseID
CourseID
这是外键
Course_table
CourseID | CourseTitle
这里的CourseID
是主键
控制器:
public function index(){
$data['news'] = $this->news_model->user_Call();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
型号:
public function user_Call(){
$query = $this->db->get('news');
$query = $this->db->get('news');
return $query->result_array();
}
查看:
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<?php endforeach ?>
答案 0 :(得分:0)
看看下面的功能我为你做了样品。试试吧。
public function user_Call()
{
$this->db->select('u.UserID,c.CourseTitle', false);
$this->db->from('User_table AS u');
$this->db->join('Course_table AS c', 'c.CourseID = u.CourseID');
$query = $this->db->get();
return $query->result_array();
}