0

CodeIgniter のモデルに、データベースに挿入して返す関数があります$result=$this->db->insert_id(); return $result;。このモデルには、別のテーブルに他の値を挿入する別の関数があり、その列に挿入する必要があります$result=$this->db->insert_id(); return $result;。しかし、私が行ったように、 function add_teacher_subject()そのために再び実行され$insert_id=$this->add_teacher_subject();ます。2番目の関数が実行されると、たとえばinsert_id = 5ではなく、insert_id = 6になります。最初の関数から2番目の関数、insert_idに挿入する方法は? これが私のコードです:

public function add_teacher_subject() {

        $date = new DateTime("now"); 
        $data=array(
            'teacher_id'=>$this->input->post('teacher'),
            'subject_id'=>$this->input->post('subject'),
            'survey_id'=>$this->uri->segment(3),
            'user_id'=>$this->session->userdata['user_id'],
            'survey_code'=>$this->input->post('random_code'),          
            'created_at'=>$date->format('Y-m-d H:i:s')

        );

        $this->db->insert('student_surveys',$data);
        $result=$this->db->insert_id();
        return $result;

    }

    public function survey_fill() 
    {
        $insert_id=$this->add_teacher_subject();
        if ($this->input->post('answer')) {
            $answers= $this->input->post('answer');           
            if (null !==($this->input->post('submit'))) {
                $date = new DateTime("now"); 
                foreach($answers as $question_id=>$answer)
                {
                    $data = array(
                        'user_id'=>$this->session->userdata['user_id'],
                        'question_id'=>$question_id,
                        'answer'=>$answer,              
                        'created_at'=>$date->format('Y-m-d H:i:s'),
                        'student_survey_id' => $insert_id //I want $insert_id from function survey_fill() How to get it
                    );

                    $this->db->insert('survey_answers', $data);

                }

            }

                if($this->db->affected_rows() > 0)
                {

                    return true;
                } 
                    return false;

        }
    } 

編集済みの質問: これらの 2 つの関数を 1 つにマージして、insert_id を最初から 2 番目に使用しようとしましたが、パラメーターを使用しましたが、それでも必要な結果が得られません。これらのパラメーターの使用方法は、最初に add_teacher_subject() を呼び出して挿入するときに必要です$this->db->insert('student_surveys',$data);。2 番目に呼び出すと、最初のクエリからその insert_id を使用します。$insert_id=$this->db->insert_id();私のモデルは次のようになります。

public function add_teacher_subject($subject=0, $survey) {

            if($subject) {
        $date = new DateTime("now"); 
        $data=array(
            'teacher_id'=>$this->input->post('teacher'),
            'subject_id'=>$this->input->post('subject'),
            'survey_id'=>$this->uri->segment(3),
            'user_id'=>$this->session->userdata['user_id'],
            'survey_code'=>$this->input->post('random_code'),          
            'created_at'=>$date->format('Y-m-d H:i:s')

        );

        $this->db->insert('student_surveys',$data);

        $insert_id=$this->db->insert_id();
}
if($survey) {



         if ($this->input->post('answer')) {
            $answers= $this->input->post('answer');           
            if (null !==($this->input->post('submit'))) {
                $date = new DateTime("now"); 
                foreach($answers as $question_id=>$answer)
                {
                    $data = array(
                        'user_id'=>$this->session->userdata['user_id'],
                        'question_id'=>$question_id,
                        'answer'=>$answer,              
                        'created_at'=>$date->format('Y-m-d H:i:s'),
                        'student_survey_id' => $insert_id
                    );

                    $this->db->insert('survey_answers', $data);

                }

            }
             return $insert_id;
                if($this->db->affected_rows() > 0)
                {

                    return true;
                } 
  }                  return false;

        }

        }

私のコントローラーは:

 public function survey_fill()
    { 
     //form_validation for teacher and subject
if ($this->form_validation->run()==FALSE)
        {

            $this->survey_show();
        }
        else 
        {
            $survey=$this->uri->segment(3);
            if ($this->user_model->add_teacher_subject($survey)) 
            {
                echo "You filled survey!";  
                echo "<script>setTimeout(\"location.href = '/survey/index.php/index/surveys_show';\",1500);</script>";          
            }
}
}
public function student_surveys() {
//form validation for survey answers
 if ($this->form_validation->run()==FALSE)
        {
            $this->student_surveys_show();
        } 
        else 
        {
            $subject=$this->input->post('subject');
            $this->user_model->add_teacher_subject($subject);
            if($this->db->affected_rows() > 0)
            {   

               $survey_id = $this->uri->segment(3);

               redirect('/index/survey_show/' . $survey_id);

            }               

        }
4

0 に答える 0