所以我得到了两个表,一个叫做patients,另一个叫做tests,tests表有patient id,我有一个叫做add patient的页面,其中有一些字段用于添加新的患者信息,还有一些字段用于添加他们的测试信息,并在一个查询中将所有数据上传到这两个表中,测试字段可以被ajax复制到我可以为同一个病人添加多个测试,现在我想在同一时间将多个测试添加到测试表中,我成功地做到了这一点,但情况是我无法将病人id添加到测试表中,我想在我添加的所有测试中多次添加相同的患者id,同时在该页面中添加新患者,我是codeigniter的新手!这是添加页面
患者字段和测试字段
<input type="text" name="patientname" />
<input type="text" name="address" />
<input type="text" name="testname[]"/>
<input type="text" name="price[]" />
这是我的控制器
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');
}
这是我的模型
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条答案
按热度按时间0lvr5msh1#
我的意思是,我不知道,但你的代码似乎是如此正确和逻辑,但我试过这个代码,它工作得很好,我甚至没有使用模型/
whitzsjs2#
首先,你不需要这个。
因为你打电话的时候
阵列,
$patient_input_data
,将被创建。有时,您可能需要确保拥有一个数组,即使它是空的。但这不是其中之一。对于数组输入值,即
testname[]
,通过去掉名称末尾的括号来获取数据。这样地。vars
$testname
以及$price
将是数组,窗体上的每个字段都有一个项。在我看来,这两个输入是必需的,所以您应该添加代码来检查情况。表单验证类非常适合这个目的。
阵列
$test_input_data
在这种情况下,您希望数组存在—即使它是空的。在向数组中添加项时,不必显式地设置索引值,即。$test_input_data[$i] = array(...
因为$test_input_data[] = array(...
会很好用,但两种方法都没有坏处。在模型上。第一部分很好。对于第二个,您需要创建包含从第一次插入中获得的患者id的数组,并将该值添加到
$tests
争论。然后模型变成这样。