返回数据为空,但是从模型设​​置它可以工作

时间:2019-08-14 06:57:42

标签: php codeigniter

我有模型方法,该函数获取数据并将其发送给视图,但是返回为null,错误在哪里?

但是当我将$nilai_l = $row['under_l'];更改为$nilai_l = 10;时 它可以正常工作,然后发送10次查看,错误在哪里?

public function get_query_summary($param = "") {
    $l = 'L';
    $p = 'P';
    $tahun_akademik = $this->session->userdata('tahun_akademik');
    if (!empty($param["academic_year"])) {
        $tahun_akademik = $param['academic_year'];
    }

    $this->db->select(''
                    . 'SUM(if((L.BERAT / (L.TINGGI * L.TINGGI / 10000)) < 18.5, 1, 0)) as under_l, '
                    . 'SUM(if((L.BERAT / (L.TINGGI * L.TINGGI / 10000)) > 18.5 AND (L.BERAT / (L.TINGGI * L.TINGGI / 10000)) < 22.9 , 1, 0)) as ideal_l,'
                    . 'SUM(if((L.BERAT / (L.TINGGI * L.TINGGI / 10000)) > 23 AND (L.BERAT / (L.TINGGI * L.TINGGI / 10000)) < 24.9 , 1, 0)) as warning_l,'
                    . 'SUM(if((L.BERAT / (L.TINGGI * L.TINGGI / 10000)) > 25 AND (L.BERAT / (L.TINGGI * L.TINGGI / 10000)) < 29.9 , 1, 0)) as obesitas1_l,'
                    . 'SUM(if((L.BERAT / (L.TINGGI * L.TINGGI / 10000)) > 30, 1, 0)) as obesitas2_l,'
                    . 'SUM(if((P.BERAT / (P.TINGGI * P.TINGGI / 10000)) < 18.5, 1, 0)) as under_p,'
                    . 'SUM(if((P.BERAT / (P.TINGGI * P.TINGGI / 10000)) > 18.5 AND (P.BERAT / (P.TINGGI * P.TINGGI / 10000)) < 22.9 , 1, 0)) as ideal_p,'
                    . 'SUM(if((P.BERAT / (P.TINGGI * P.TINGGI / 10000)) > 23 AND (P.BERAT / (P.TINGGI * P.TINGGI / 10000)) < 24.9 , 1, 0)) as warning_p,'
                    . 'SUM(if((P.BERAT / (P.TINGGI * P.TINGGI / 10000)) > 25 AND (P.BERAT / (P.TINGGI * P.TINGGI / 10000)) < 29.9 , 1, 0)) as obesitas1_p,'
                    . 'SUM(if((P.BERAT / (P.TINGGI * P.TINGGI / 10000)) > 30, 1, 0)) as obesitas2_p,'
                    . 'SUM(P.BERAT / (P.TINGGI * P.TINGGI / 10000)) as total')
            ->from('m_siswa A')
            ->join('tb_medical_wh L', "A.ID = L.ID_SISWA AND A.JENIS_KELAMIN = '$l' AND L.TAHUN_AKADEMIK = '$tahun_akademik'", 'left')
            ->join('tb_medical_wh P', "A.ID = P.ID_SISWA AND A.JENIS_KELAMIN = '$p' AND P.TAHUN_AKADEMIK = '$tahun_akademik'", 'left');
}

public function get_list_html_summary($param) {
    $this->get_query_summary($param);
    $data = $this->get_datatables();

    $records["list"] = [];
    $month = array('Obesitas' => 0, 'Obesitas awal' => 0, 'Warning' => 0, 'Ideal' => 0, 'Underweight' => 0);

    foreach ($month as $key => $row) {
        $nilai_l = 0;
        $nilai_p = 0;
        $total = 0;
        switch ($key) {
            case 'Underweight':
                $nilai_l = $row['under_l'];
                $nilai_p = $row['under_p'];
                //$total     = $row['under_l']+$row['under_p'];
                break;

            case 'Ideal':
                $nilai_l = $row['ideal_l'];
                $nilai_p = $row['ideal_p'];
                //$total   = 23;   
                break;

            case 'Warning';
                $nilai_l = $row['warning_l'];
                $nilai_p = $row['warning_p'];
                //$total   = 33;
                break;

            case 'Obesitas awal';
                $nilai_l = $row['obesitas1_l'];
                $nilai_p = $row['obesitas1_p'];
                //$total   = 43;
                break;

            case 'Obesitas';
                $nilai_l = $row['obesitas2_l'];
                $nilai_p = $row['obesitas2_p'];
                //$total   = 53;
                break;
        }
        $records["list"][] = array(
            $key,
            $nilai_l,
            $nilai_p,
            $total
        );
    }
    return $records;
}

1 个答案:

答案 0 :(得分:0)

您正在分配$row数据,因为它仅包含几个月的数据,所以它当然不会起作用。尝试将get_query_summary()方法返回的数据分配给变量(例如$student_data),并且您还需要get()result_array()方法来获取数组结果:< / p>

public function get_query_summary($param = "") {
    $l = 'L';
    $p = 'P';
    $tahun_akademik = $this->session->userdata('tahun_akademik');
    if (!empty($param["academic_year"])) {
        $tahun_akademik = $param['academic_year'];
    }

    $this->db->select(''
                    . 'SUM(if((L.BERAT / (L.TINGGI * L.TINGGI / 10000)) < 18.5, 1, 0)) as under_l, '
                    . 'SUM(if((L.BERAT / (L.TINGGI * L.TINGGI / 10000)) > 18.5 AND (L.BERAT / (L.TINGGI * L.TINGGI / 10000)) < 22.9 , 1, 0)) as ideal_l,'
                    . 'SUM(if((L.BERAT / (L.TINGGI * L.TINGGI / 10000)) > 23 AND (L.BERAT / (L.TINGGI * L.TINGGI / 10000)) < 24.9 , 1, 0)) as warning_l,'
                    . 'SUM(if((L.BERAT / (L.TINGGI * L.TINGGI / 10000)) > 25 AND (L.BERAT / (L.TINGGI * L.TINGGI / 10000)) < 29.9 , 1, 0)) as obesitas1_l,'
                    . 'SUM(if((L.BERAT / (L.TINGGI * L.TINGGI / 10000)) > 30, 1, 0)) as obesitas2_l,'
                    . 'SUM(if((P.BERAT / (P.TINGGI * P.TINGGI / 10000)) < 18.5, 1, 0)) as under_p,'
                    . 'SUM(if((P.BERAT / (P.TINGGI * P.TINGGI / 10000)) > 18.5 AND (P.BERAT / (P.TINGGI * P.TINGGI / 10000)) < 22.9 , 1, 0)) as ideal_p,'
                    . 'SUM(if((P.BERAT / (P.TINGGI * P.TINGGI / 10000)) > 23 AND (P.BERAT / (P.TINGGI * P.TINGGI / 10000)) < 24.9 , 1, 0)) as warning_p,'
                    . 'SUM(if((P.BERAT / (P.TINGGI * P.TINGGI / 10000)) > 25 AND (P.BERAT / (P.TINGGI * P.TINGGI / 10000)) < 29.9 , 1, 0)) as obesitas1_p,'
                    . 'SUM(if((P.BERAT / (P.TINGGI * P.TINGGI / 10000)) > 30, 1, 0)) as obesitas2_p,'
                    . 'SUM(P.BERAT / (P.TINGGI * P.TINGGI / 10000)) as total')
            ->from('m_siswa A')
            ->join('tb_medical_wh L', "A.ID = L.ID_SISWA AND A.JENIS_KELAMIN = '$l' AND L.TAHUN_AKADEMIK = '$tahun_akademik'", 'left')
            ->join('tb_medical_wh P', "A.ID = P.ID_SISWA AND A.JENIS_KELAMIN = '$p' AND P.TAHUN_AKADEMIK = '$tahun_akademik'", 'left');
    return $this->db->get('m_siswa A')->result_array();

}
public function get_list_html_summary($param) {
    $student_data = $this->get_query_summary($param);
    $data = $this->get_datatables();

    $records["list"] = [];
    $month = array('Obesitas' => 0, 'Obesitas awal' => 0, 'Warning' => 0, 'Ideal' => 0, 'Underweight' => 0);

    foreach ($month as $key => $row) {
        $nilai_l = 0;
        $nilai_p = 0;
        $total = 0;
        switch ($key) {
            case 'Underweight':
                $nilai_l = $student_data['under_l'];
                $nilai_p = $student_data['under_p'];
                //$total     = $student_data['under_l']+$student_data['under_p'];
                break;

            case 'Ideal':
                $nilai_l = $student_data['ideal_l'];
                $nilai_p = $student_data['ideal_p'];
                //$total   = 23;   
                break;

            case 'Warning';
                $nilai_l = $student_data['warning_l'];
                $nilai_p = $student_data['warning_p'];
                //$total   = 33;
                break;

            case 'Obesitas awal';
                $nilai_l = $student_data['obesitas1_l'];
                $nilai_p = $student_data['obesitas1_p'];
                //$total   = 43;
                break;

            case 'Obesitas';
                $nilai_l = $student_data['obesitas2_l'];
                $nilai_p = $student_data['obesitas2_p'];
                //$total   = 53;
                break;
        }
        $records["list"][] = array(
            $key,
            $nilai_l,
            $nilai_p,
            $total
        );
    }
    return $records;
}