Php 获取数据库中的数据并合并重复数据
我想使用Php 获取数据库中的数据并合并重复数据,php,mysql,sql,codeigniter,Php,Mysql,Sql,Codeigniter,我想使用CodeIgniter从数据库中获取数据 function fetch_chapter() { $this->db->select('*'); $this->db->from("chapter"); $this->db->join('unit', 'unit.unit_number = chapter.unit_number', 'left'); $this->db->order_by('c
CodeIgniter
从数据库中获取数据
function fetch_chapter()
{
$this->db->select('*');
$this->db->from("chapter");
$this->db->join('unit', 'unit.unit_number = chapter.unit_number', 'left');
$this->db->order_by('chapter.unit_number', 'ASC');
$this->db->order_by('chapter_number', 'ASC');
$query = $this->db->get();
return $query;
}
我希望得到如下结果:
Unit 1
Chapter 1
Lesson 1
Lesson 2
Chapter 2
Lesson 1
Unit 2
Chapter 1
Lesson 1
Chapter 2
Lesson 1
这可能是您的控制器代码:
$this->load->model('modelname');
$data['s'] = $this->modelname->fetch_chapter();
$this->load->view('foldername/filename', $data, FALSE);
您的型号代码:
function fetch_chapter()
{
$this->db->select('*');
$this->db->distinct( );
$this->db->from("chapter");
$this->db->join('unit', 'unit.unit_number = chapter.unit_number', 'left');
$this->db->order_by('chapter.unit_number', 'ASC');
$this->db->order_by('chapter_number', 'ASC');
$query = $this->db->get();
return $query->row();
}
每个文本框中的查看页面代码:
<input type="text" value="<?php echo $s->fieldname; ?>" >
$this->db->distinct();使用此行$this->db->group_by(“列名称”);当我使用组_时,每个单元只返回一章。你的问题需要更多关注(太广泛)。请注意您是如何请求有关模型的帮助的,然后在评论中,您需要有关控制器的帮助。这是一种志愿者不喜欢被卷入的场景,因为它是“永远不会结束的问题”,也就是“请给我写一个完整的教程”。你获取大量数据或单行,你必须说你的模型代码有完整的问题,我帮不了你。没有错误。。输出为单元1第1章单元1第2章单元2第1章单元2第2章。。单元仍在重复。