通过循环传递数组时出错

时间:2014-09-01 07:31:22

标签: codeigniter foreach

这是我的控制器功能

public function update_monthly() {
        $data = $this->student_model->get_due_info();

        foreach ($data as $value) {
            $due = $this->student_model->get_due_by_id($value);
            $grade = $this->student_model->get_grade_by_id($value);
            $grade_due = $this->student_model->get_due_by_grade($grade);
            $new_due = $due + $grade_due;
            $this->db->set('md_due',$new_due, FALSE);
        }   
    }

我收到以下错误

    A PHP Error was encountered

    Severity: Notice

    Message: Array to string conversion

    Filename: database/DB_active_rec.php

    Line Number: 427

A Database Error Occurred

Error Number: 1054

Unknown column 'Array' in 'where clause'

SELECT `md_due` FROM (`monthly_due`) WHERE `md_student_id` = Array

Filename: F:\xampp\htdocs\student\system\database\DB_driver.php

Line Number: 330

型号代码

public function get_due_by_id($id) {
        $this->db->select('md_due');
        $this->db->from('monthly_due');
        $this->db->where('md_student_id', $id);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function get_due_by_grade($id) {
        $this->db->select('dt_fee');
        $this->db->from('due_table');
        $this->db->where('dt_grade', $id);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function get_grade_by_id($id) {
        $this->db->select('grade');
        $this->db->from('student_info');
        $this->db->where('student_gen_id', $id);
        $query = $this->db->get();
        return $query->result_array();
    }
    public function get_due_info() {

        $this->db->select('md_student_id');
        $this->db->from('monthly_due');

        $query = $this->db->get();
        return $query->result_array();
    }

2 个答案:

答案 0 :(得分:0)

您将整个数组传递给您的模型而不仅仅是您想要的值

$due = $this->student_model->get_due_by_id($value);

正确的方法是通过

$value['md_student_id'] 

这是您实际想传递的值

用这个

替换你的代码
foreach ($data as $value) {
            $due = $this->student_model->get_due_by_id($value['md_student_id'] );
            $grade = $this->student_model->get_grade_by_id($value['md_student_id']);
            $grade_due = $this->student_model->get_due_by_grade($grade[0]['grade']);
            $new_due = $due + $grade_due;
            $this->db->set('md_due',$new_due, FALSE);
        }   

答案 1 :(得分:0)

$value['md_student_id']传递给模型而不是控制器中的$value

$data = $this->student_model->get_due_info();
foreach ($data as $value) {
    $due = $this->student_model->get_due_by_id($value['md_student_id'] );
    $grade = $this->student_model->get_grade_by_id($value['md_student_id']);
    $grade_due = $this->student_model->get_due_by_grade($grade[0]['grade']);
    //Add remaining logic
}