首页 > 解决方案 > 获取数据库中的数据并合并重复数据

问题描述

我想使用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

标签: phpmysqlsqlcodeigniter

解决方案


可能是您的控制器代码:

$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; ?>" >

推荐阅读