doru52911
doru52911
2018-12-02 23:31

使用Codeigniter将最后插入的记录ID插入另一个表中的数组中

已采纳

so I got two tables one called patients and another called tests , the tests table has the patient id , i have a page called add patient which have fields for adding new patient information and other fields for adding their test info and upload all the data into the two tables in one query ,tests fields could be duplicated by ajax to i could add more than one test to the same patient , now i wanna add more than one test at the same time into the tests table and i managed to do so but the case is i can't add the patient_id into tests table, i wanna add the same patient_id more than one time to all the tests i have added while adding a new patient in that page, i'm new to Codeigniter! this is the adding page

the patient fields and the test fields

<input type="text" name="patientname" />
<input type="text" name="address" />

<input type="text" name="testname[]"/>
<input type="text" name="price[]" />

and this is my controller

public function testbby
{
    $this->load->model("khmodel", "Khmodel");

    // patient main info
    $patient_input_data = array();
    $patient_input_data['patientname'] = $this->input->post('patientname');
    $patient_input_data['address'] = $this->input->post('address');

    //test data
    $testname = $this->input->post('testname[]');
    $price = $this->input->post('price[]');

    $test_input_data = array();
    for ($i = 0; $i < count($testname); $i ++ )
    {
        $test_input_data[$i] = array(
            'testname' => $testname[$i],
            'price' => $price[$i],
        );
    }
    $this->Khmodel->insert_bby($patient_input_data, $test_input_data);

    redirect('main/dashboard');
}

and this is my model

public function insert_bby($patient, $test)
{
    $this->db->insert('patients', $patient);
    $patient_id = $this->db->insert_id();

    // i used this and it worked but only while adding one test , s
    //once it's gonna be an array i dunno what to do with the id !
    //$test['refpatient']=$patient_id;
    $this->db->insert_batch('tests', $test);

    return $insert_id = $this->db->insert_id();
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongti8535 dongti8535 3年前

    the value i mean , i dont know but your code seems to be so right and logical but i have tried this code and it worked so well , i didn't even use the model/

     public function testbby
    {
    $this->load->model("khmodel", "Khmodel");
    
    // patient main info
    $patient_input_data = array();
    $patient_input_data['patientname'] = $this->input->post('patientname');
    $patient_input_data['address'] = $this->input->post('address');
    
    //test data
    $testname = $this->input->post('testname[]');
    $price = $this->input->post('price[]');
    
        $this->db->reset_query();
        $this->db->insert('patients', $patient_input_data);
        $patient_id=$this->db->insert_id();
    $test_input_data = array();
    for ($i = 0; $i < count($testname); $i ++ )
    {
        $test_input_data[] = array(
            'testname' => $testname[$i],
            'price' => $price[$i],
            'patient_id'=>$patient_id
    
        );
    }
      $this->db->reset_query();
      $this->db->insert_batch('tbl_tests',$test_input_data);
       redirect('main/dashboard');
       }
    
    点赞 评论 复制链接分享
  • douhan4243 douhan4243 3年前

    To start with, you don't need this.

    $patient_input_data = array();
    

    Because when you make the call

    $patient_input_data['patientname'] = $this->input->post('patientname');
    

    The array, $patient_input_data, will be created. There are times when you might want to make sure you have an array even if it's empty. But this isn't one of them.

    For the array input values, ie testname[], get the data by leaving off the brackets at the end of the name. Like this.

    //test data
    $testname = $this->input->post('testname'); //instead of post('testname[]')
    $price = $this->input->post('price');
    

    The vars $testname and $price will be arrays with an item for each field on the form.

    Seems to me that those two inputs are required so you should add code to check that is the case. The Form Validation class is excellent for that purpose.

    The array $test_input_data is a case where you will want the array to exist - even if it's empty. You don't have to explicitly set the index value when adding items to the array, i.e. $test_input_data[$i] = array(... because $test_input_data[] = array(... will work just fine, but there's no harm either way.

    On to the model. The first part is good. For the second you need to create arrays that include the patient id you got from the first insert and add that value to each of the sub-arrays in the $tests argument. The model then becomes this.

    public function insert_bby($patient, $tests)
    {
        $this->db->insert('patients', $patient);
        $patient_id = $this->db->insert_id();
        // add the patient id key/value to each sub-array in $tests
        foreach ($tests as $test)
        {
            $test['patient id'] = $patient_id;
        }
        // will return the number of rows inserted or FALSE on failure
        return $this->db->insert_batch('tests', $tests);
    }
    
    点赞 评论 复制链接分享

相关推荐