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