使用动态ID批量更新Codeigniter

dgtucam1  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(143)

我正在尝试按ID批更新表
我的控制器:

if(customCompute($this->data['student'])) {
                            $studentextendID = $this->data['student']->studentextendID;
                            $this->data['students'] = $this->studentextend_m->get_studentextend(array('studentextendID' => $studentextendID));
                            } else {
                                $this->data['students'] = [];
                        }
        

                        $post = $this->input->post();
                        for($i=0; $i < count($post['subject']); $i++) {
                            $studentExtendArray[] = array(
                                'studentextendID' => $studentextendID,
                                'studentID' => $studentID,
                                'subject' => $post['subject'][$i],
                                'subjectng' => $post['subjectng'][$i],
                                'subjectlg' => $post['subjectlg'][$i],
                                'subjectcre' => $post['subjectcre'][$i],
    
    
                            );
                        $this->db->update_batch('studentextend', $studentExtendArray, 'studentextendID');

                        }

我的模型

function get_studentextend($array=NULL, $signal=FALSE) {
    $query = parent::get($array, $signal);
    return $query;
}

数组输出:

Array (
[0] => Array
    (
        [studentextendID] => 143
        [studentID] => 97
        [subject] => 
        [subjectng] => 5235
        [subjectlg] => 5231
        [subjectcre] => 523155
    )

[1] => Array
    (
        [studentextendID] => 143
        [studentID] => 97
        [subject] => 
        [subjectng] => 2
        [subjectlg] => 99
        [subjectcre] => 3
    ) )

如您所见,'studentextendID'在两个数组上都是重复的,但它应该是动态取得的,例如:143和144,因为表中有两行具有相同的'studentID'

2wnc66cl

2wnc66cl1#

您可以尝试更改模型

public function update_batchs($table, $data)
{
    if ($data) {
        return $this->db->update_batch($table, $data, 'studentextendID');
    }
}
ig9co6j1

ig9co6j12#

您没有在for循环中传递$studentextendID。您需要在for循环中传递它。

$studentextendID = $this->data['student']->studentextendID;

同时请验证$this-〉data ['student']。

相关问题