php - 我想使用 codeigniter 对我的结果进行一些修正

标签 php mysql codeigniter codeigniter-2

now the output is getting like this. i marked what i needed

某些行具有相同的日期。我想把它作为一个整体。例如:2016-11-02,下午英语 1 和中午英语 11。现在分为两个条目。请帮我将其设为一行。我在这里分享我的模型功能

型号

public function select_data() {
    $this->db->distinct();
    $this->db->select('extt_std as std');
    $this->db->from('exam_time_table');
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $row->standard = $row->std;
            $data[] = $row;

            $this->db->select('*');
            $this->db->from('exam_time_table');
            $this->db->where('exam_time_table.extt_std', $row->std);
            $query = $this->db->get();
            $get = $query->result();

            foreach ($get as $row) {
                if ($row->extt_sess == 'FN') {
                    $row->fornoon = $row->extt_sub;
                    $row->afternoon = '';
                }
                if ($row->extt_sess == 'AN') {
                    $row->fornoon = '';
                    $row->afternoon = $row->extt_sub;
                }
                $data[]=$row;
            }
        }
        return $data;
    }
    return false;
}

我的 table

CREATE TABLE `exam_time_table` (
  `extt_id` int(10) NOT NULL,
  `extt_date` date NOT NULL,
  `extt_exam` varchar(20) NOT NULL,
  `extt_std` varchar(10) NOT NULL,
  `extt_sub` varchar(15) NOT NULL,
  `extt_sess` varchar(10) NOT NULL,
  `extt_year` varchar(10) NOT NULL
) 

INSERT INTO `exam_time_table` (`extt_id`, `extt_date`, `extt_exam`, `extt_std`, `extt_sub`, `extt_sess`, `extt_year`) VALUES
(1, '2016-11-01', 'Half Yearly', 'I-STD', 'Tamil', 'FN', '16-17'),
(2, '2016-11-01', 'Half Yearly', 'II-STD', 'Tamil', 'FN', '16-17'),
(3, '2016-11-01', 'Half Yearly', 'III-STD', 'Tamil', 'FN', '16-17'),
(4, '2016-11-01', 'Half Yearly', 'IV-STD', 'Tamil', 'FN', '16-17'),
(5, '2016-11-01', 'Half Yearly', 'XI-STD', 'Tamil-I', 'FN', '16-17'),
(6, '2016-11-01', 'Half Yearly', 'X-STD', 'Tamil-I', 'FN', '16-17'),
(7, '2016-11-01', 'Half Yearly', 'V-STD', 'Tamil', 'FN', '16-17'),
(8, '2016-11-01', 'Half Yearly', 'VII-STD', 'Tamil', 'FN', '16-17'),
(9, '2016-11-01', 'Half Yearly', 'VII-STD', 'Tamil', 'FN', '16-17'),
(10, '2016-11-01', 'Half Yearly', 'VIII-STD', 'Tamil', 'FN', '16-17'),
(11, '2016-11-01', 'Half Yearly', 'IX-STD', 'Tamil-II', 'AN', '16-17'),
(12, '2016-11-01', 'Half Yearly', 'X-STD', 'Tamil-II', 'AN', '16-17'),
(13, '2016-11-02', 'Half Yearly', 'I-STD', 'English', 'AN', '16-17'),
(14, '2016-11-02', 'Half Yearly', 'II-STD', 'English', 'FN', '16-17'),
(15, '2016-11-02', 'Half Yearly', 'IX-STD', 'English-I', 'FN', '16-17'),
(16, '2016-11-02', 'Half Yearly', 'IX-STD', 'English-II', 'AN', '16-17'),
(17, '2016-11-02', 'Half Yearly', 'X-STD', 'English-I', 'FN', '16-17'),
(18, '2016-11-02', 'Half Yearly', 'X-STD', 'English-II', 'AN', '16-17'),
(19, '2016-11-02', 'Half Yearly', 'III-STD', 'English', 'FN', '16-17');

最佳答案

这有点复杂,但可以解决您的问题。如果其他人可以提供更好的解决方案。我将不胜感激。

public function select_data() {
$this->db->distinct();
$this->db->select('extt_std as std');
$this->db->from('exam_time_table');
$query = $this->db->get();
if ($query->num_rows() > 0) {
    foreach ($query->result() as $row) {
        $row->standard = $row->std;
        $data[] = $row;

        $this->db->select('*');
        $this->db->from('exam_time_table');
        $this->db->where('exam_time_table.extt_std', $row->std);
        $this->db->where('exam_time_table.extt_sess', 'AN');
        $subquery = $this->db->get_compiled_select();

        $this->db->select('*');
        $this->db->from('exam_time_table');
        $this->db->where('exam_time_table.extt_std', $row->std);
        $this->db->where('exam_time_table.extt_sess', 'FN');
        $subquery2 = $this->db->get_compiled_select();

        $this->db->select('a.*,b.*');
        $this->db->from('('.$subquery.') a');
        $this->db->join('('.$subquery2.') b','a.extt_std=b.extt_std','');
        $get = $this->db->result();

        foreach ($get as $row) {
            if ($row->extt_sess == 'FN') {
                $row->fornoon = $row->extt_sub;
                $row->afternoon = '';
            }
            if ($row->extt_sess == 'AN') {
                $row->fornoon = '';
                $row->afternoon = $row->extt_sub;
            }
            $data[]=$row;
        }
    }
    return $data;
}
return false;
}

关于php - 我想使用 codeigniter 对我的结果进行一些修正,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40219516/

相关文章:

phpunit 仅为特定文件夹生成 html 格式的覆盖率报告

php - 如何在 PHP 中使用循环表中的 implode?

mysql - sql查询字符串中的整数

php - mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows 等...期望参数 1 是资源

php - Codeigniter 数据库加入额外的变量

javascript - 使用数据库中的值循环选项元素

php - Symfony2 多种形式在不同的 JQuery UI 选项卡但单页

php - 将 MySQL 集结果存储在 PHP session 数组中以减少数据库查找

mysql - 使用 SpringMvc hibernate MySql 和 maven 的基本 CRUD 应用程序

mysql - 我可以从 .sql 创建初始 CodeIgniter 数据库迁移吗?