gpt4 book ai didi

php - CodeIgniter 打印关系数据

转载 作者:行者123 更新时间:2023-11-30 23:03:57 24 4
gpt4 key购买 nike

我正在查看 CodeIgniter 文档,只找到一种方法来获取整个表,我正在寻找一种方法来获取 UserIDCourseTitle来自不同但相关表的一行中的列。

我有这个表:

用户表

UserID | CourseID

CourseID这里是外键

类(class)表

CourseID | CourseTitle

这里的CourseID为主键

Controller :

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 ?>

最佳答案

看看下面我为您制作的函数示例。试试吧。

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();
}

关于php - CodeIgniter 打印关系数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22683500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com