How to use the same insert_id() in CodeIgniter from one function to another function

45 Views Asked by At

I have a function in my model in CodeIgniter where I insert in Database and I return $result=$this->db->insert_id(); return $result;. I have another function in this model where I insert other values in another table and I need to insert in a column that $result=$this->db->insert_id(); return $result;. But as I've done it, function add_teacher_subject() runs again because of that: $insert_id=$this->add_teacher_subject(); and when second function execute I have for example not insert_id=5, but insert_id=6. How to insert in second function, insert_id from the first function? Here's my code:

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;

        }
    } 

Еdited question: I tried to merge these 2 functions in one to use insert_id from first in second and I used parameters, but still it doesn't have the result I want. How to use these parameters, I want when I first call add_teacher_subject() to insert $this->db->insert('student_surveys',$data);. When second I call it, to use that insert_id from first query: $insert_id=$this->db->insert_id();My model now is:

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;

        }

        }

My controller is:

 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);

            }               

        }
0

There are 0 best solutions below