【问题标题】:Subtract the two sum in foreach loop for datatable为数据表减去 foreach 循环中的两个总和
【发布时间】:2019-03-26 02:12:52
【问题描述】:

到目前为止,我如何从数据库中减去 foreach 循环中获取的数据的两个总和,以使用 codeigniter 显示在数据表中,这就是我所做的......

function fetch_user(){
            $fetch_data = $this->point_m->make_datatables();
            $data = array();
            foreach ($fetch_data as $row) 
            {
                $sub_array = array();
                $sub_array[] = $row->idnumber;
                $sub_array[] = $row->firstname;
                $sub_array[] = $row->middlename;
                $sub_array[] = $row->lastname;
                $sub_array[] = $row->course_id;
                $sub_array[] = $row->schoolyear;
                $sub_array[] = $row->semester;
                $sub_array[] = $row->point;
                $sub_array[] = $row->redeem;
                $data[] = $sub_array;
            }

            $output = array(
                "draw" => $_POST['draw'],
                "recordsTotal" => $this->point_m->get_all_data(),
                "recordsFiltered" => $this->point_m->get_filtered_data(),
                "data" => $data
            );
            echo json_encode($output);
        }

$row->point 和 $row->redeem 已经相加,现在我想知道我是否可以减去这两个($row->point,$row->redeem) 以及如何..

这是我的查询

$query = $this->db
            ->select("students.id,idnumber, firstname, middlename, lastname, course_id, schoolyear, semester, sum(point) as 'point', sum(redeem) as 'redeem'")
            ->distinct()
            ->from('students')
            ->join('section_student', 'students.id = section_student.student_id', 'LEFT')
            ->group_by('idnumber, firstname, middlename, lastname, course_id, schoolyear, semester')
            ->where('schoolyear','schoolyear')
            ->where('semester','semester');
        $query = $this->db->get();
        return $query->result();

【问题讨论】:

  • 似乎它们是不同的值。它们在哪里求和?
  • 在模型中添加我的查询,先生

标签: php jquery foreach datatable codeigniter-3


【解决方案1】:

我只是添加另一个 sub_array 并减去这两个

$sub_array[] = $row->point - $row->redeem;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 2021-12-03
    • 2018-01-31
    • 2020-10-11
    相关资源
    最近更新 更多