0

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

            }               

        }
Sparky
  • 98,165
  • 25
  • 199
  • 285
ci_lover
  • 710
  • 1
  • 14
  • 38
  • I cannot understand clearly But your `survey_fill` function has `$insert_id`, You can use it again.Its the same value. – Shaiful Islam May 01 '15 at 08:44
  • This $insert_id I take from the function add_teacher_student(). That is my goal. I want to insert insert_id from function add_teacher_student in function survey_fill. When I use it like that: $insert_id=$this->add_teacher_student, this function runs again and $insert_id is not for example =3, but =4 – ci_lover May 01 '15 at 09:15
  • Still cannot understand and made more confusion where you saying `add_teacher_student()` function and I don't see that function in your question. – Shaiful Islam May 01 '15 at 09:20
  • add_teacher_subject, sorry :) – ci_lover May 01 '15 at 09:21
  • `$this->db->insert_id();` always return last inserted id into your database.If you want to use previous inserted_id you need to save it somewhere(may be inside variable) – Shaiful Islam May 01 '15 at 09:21
  • I want to use this $this->db->insert_id() in my function survey_fill for column: 'student_survey_id' . Because this $insert_id is from another function - add_teacher_subject(), I don't know how to use it in function survey_fill(). – ci_lover May 01 '15 at 09:23
  • can you improve your question with commenting the code where you want- what. – Shaiful Islam May 01 '15 at 09:23
  • Please try to understand that this site's "code snippets" feature is only for live demos of HTML-CSS-JavaScript. You cannot run PHP within your OP as if it's JavaScript. Edited OP. Thanks. – Sparky May 01 '15 at 13:41

0 Answers0