Php Codeigniter:更新多行
我正在使用Codeigniter 2,我想更新多行 型号 这是我的模型,但问题是,只更新了第一行Php Codeigniter:更新多行,php,codeigniter,Php,Codeigniter,我正在使用Codeigniter 2,我想更新多行 型号 这是我的模型,但问题是,只更新了第一行 foreach ($this->input->post('qty') as $key => $value) { $data = array( 'item_quantity' => $value, 'discount' => $this->input->pos
foreach ($this->input->post('qty') as $key => $value) {
$data = array(
'item_quantity' => $value,
'discount' => $this->input->post('discount')[$key],
'senior' => $this->input->post('qty')[$key]
);
$this->db->where('item_id', $this->input->post('item_id')[$key]);
$this->db->where('request_id', $this->input->post('request_id'));
return $this->db->update('medical_request_items', $data);
}
另一方面,我设法做到了这一点
foreach ($query->result() as $row)
{
echo '<tr>';
echo '<input type="hidden" name="item_id[]" value="' . $row->item_id . '">';
echo '<td>' . $x++ . '</td>';
echo '<td>' . $row->item_id . '</td>';
echo '<td>' . $row->item_name . '</td>';
echo '<td><input type="text" size="1" name="qty[]" value="' . $row->item_quantity . '"></td>';
echo '<td>' . $row->item_retailprice . '</td>';
echo '<td><input type="text" size="2" name="discount[]" value="' . $row->discount . '"></td>';
echo '<td><input type="text" size="2" name="senior[]" value="' . $row->senior . '"></td>';
echo '<td>' . ($row->item_quantity * $row->item_retailprice) . '</td>';
echo '<td>' . (($row->item_quantity * $row->item_retailprice)-($row->item_quantity * $row->discount)-($row->item_quantity * $row->senior)) . '</td>';
echo '</tr>';
}
您正在发布多个数组,但在您的模型中,您只在一个数组中循环,因此where子句将始终相同 您正在发布多个数组,但在您的模型中,您只在一个数组中循环,因此您的where子句将始终相同 您完全可以一次查看并更新一条记录。但我认为还应该能够使用更新批处理一次完成所有这些任务
$this->db->update_batch('yourTableName', $updateData, 'field_to_use_for_key');
ci 2的信息
====
好的,让我们深入研究代码
// define an array
$requests = array() ;
foreach ($this->input->post('qty') as $key => $value) {
// notice the brackets, very important $requests[]
// this will add to the same array in each loop
$requests[] = array(
'request_id' => $this->input->post('request_id'),
'item_id' => $this->input->post('item_id')[$key],
'item_quantity' => $value,
'discount' => $this->input->post('discount')[$key],
'senior' => $this->input->post('senior')[$key]
) ;
}
// for testing only -- echo out your array so you can confirm its all good
echo 'here is requests array: <br><pre>' ;
print_r($requests) ;
echo '</pre>' ;
// use one field to act as the key
$this->db->update_batch('medical_request_items', $requests, 'request_id');
//定义一个数组
$requests=array();
foreach($this->input->post('qty')作为$key=>$value){
//注意括号,非常重要的$requests[]
//这将添加到每个循环中的相同数组中
$requests[]=数组(
'request\u id'=>this->input->post('request\u id'),
'item_id'=>$this->input->post('item_id')[$key],
“项目数量”=>$value,
“折扣”=>$this->input->post('discount')[$key],
“高级”=>$this->input->post('senior')[$key]
) ;
}
//仅用于测试--回显您的阵列,以便确认其是否正常
echo“这是请求数组:
”;
打印(请求);
回声';
//使用一个字段作为键
$this->db->update_batch('medical_request_items',$requests,'request_id');
所以这是用一把钥匙。如果需要不止一个键或where条件来进行更新,那么只需在foreach循环中进行更新。当然,在更新用户数据之前,您正在验证任何用户数据。您完全可以每次查看并更新一条记录。但我认为还应该能够使用更新批处理一次完成所有这些任务
$this->db->update_batch('yourTableName', $updateData, 'field_to_use_for_key');
ci 2的信息
====
好的,让我们深入研究代码
// define an array
$requests = array() ;
foreach ($this->input->post('qty') as $key => $value) {
// notice the brackets, very important $requests[]
// this will add to the same array in each loop
$requests[] = array(
'request_id' => $this->input->post('request_id'),
'item_id' => $this->input->post('item_id')[$key],
'item_quantity' => $value,
'discount' => $this->input->post('discount')[$key],
'senior' => $this->input->post('senior')[$key]
) ;
}
// for testing only -- echo out your array so you can confirm its all good
echo 'here is requests array: <br><pre>' ;
print_r($requests) ;
echo '</pre>' ;
// use one field to act as the key
$this->db->update_batch('medical_request_items', $requests, 'request_id');
//定义一个数组
$requests=array();
foreach($this->input->post('qty')作为$key=>$value){
//注意括号,非常重要的$requests[]
//这将添加到每个循环中的相同数组中
$requests[]=数组(
'request\u id'=>this->input->post('request\u id'),
'item_id'=>$this->input->post('item_id')[$key],
“项目数量”=>$value,
“折扣”=>$this->input->post('discount')[$key],
“高级”=>$this->input->post('senior')[$key]
) ;
}
//仅用于测试--回显您的阵列,以便确认其是否正常
echo“这是请求数组:
”;
打印(请求);
回声';
//使用一个字段作为键
$this->db->update_batch('medical_request_items',$requests,'request_id');
所以这是用一把钥匙。如果需要不止一个键或where条件来进行更新,那么只需在foreach循环中进行更新。当然,您也在更新之前验证任何用户数据请问$\u POST包含哪些内容?请显示结构。$\u POST包含什么内容?请显示结构。我必须再次循环吗?我必须再次循环吗?我在如何设置项目的数组()方面遇到了困难。请查收邮件。我已经更新了itI。我在如何设置项目的数组()方面遇到了困难。请查收邮件。我已经更新了