CodeIgniter update_batch 不替换以前的数据,下一个更新的值将以逗号分隔作为增量
CodeIgniter update_batch without replacing the previous data next updated value will puts by comma separated as increment
虽然我不确定。 update_batch 是否可以不替换以前的数据,下一个更新的值将以逗号分隔作为数组形式放在 SAME FILED 上。为了更好地理解,我展示了 2 个演示图像。我的代码已经 运行 仅用于批量更新但不是 update_batch 增加值 而不替换以前的数据!
例如-
对于第一次更新,列字段将在 "t_franchise_price" 列 显示如下 ---
第二次执行下一次更新时,列字段 "t_franchise_price" 将如下所示 ---
我的控制器是
public function batch_update() {
$sID = $this - > input - > post('test_id[]');
$sAmt = $this - > input - > post('test_priceUpdt[]');
$sOfficeId = $this - > input - > post('fran_office_id');
date_default_timezone_set('Asia/Kolkata');
$edited_test = array();
if (!empty($sID)) {
foreach($sID as $k => $v) {
$edited_test = array(
'test_id' => $sID[$k],
'test_priceUpdt' => $sAmt[$k],
'fran_office_id' => $sOfficeId,
'last_edit_date' => date('Y-m-d H:i:s', time())
);
$edited_test = json_encode($edited_test);
$postData[] = array('test_id' => $v, 't_franchise_price' => $edited_test);
}
$data['franchise_tst_pkg'] = (object) $postData;
}
#-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - #
if ($this - > form_validation - > run() === true) {
$this - > franchise_price_model - > batchTestupdate($postData);
$this - > session - > set_flashdata('message', display('save_successfully'));
redirect('branch/franchise_price/create');
}
**我的模特是**
public function batchTestupdate($data = [])
{
$this->db
->update_batch($this->table_test, $data , 'test_id');
}
查看
<tr>
<td>
<input type="text" name="test_name[]" class="form-control" placeholder="" value="<?php echo $subject->test_name; ?>" disabled>
<input type="hidden" name="test_id[]" class="form-control" placeholder="" value="<?php echo $subject->test_id; ?>">
</td>
<td>
<input type="text" name="test_price[]" class="form-control" placeholder="" value="<?php echo $subject->test_price; ?>" disabled>
</td>
<td>
<input type="text" name="test_priceUpdt[]" class="form-control" placeholder="" value="<?php echo $subject->test_price; ?>" id="test_priceEdt">
</td>
</tr>
追加到 table 字段 t_franchise_price
而不是覆盖。
变化:
$this - > franchise_price_model - > batchTestupdate($postData);
收件人:
$this->franchise_price_model->batchTestupdate($postData, 2);
将您的模型方法更改为:
public function batchTestupdate($data = [], $method=1){
$db_table = $this->db->dbprefix($this->table_test);
$sql = "";
if($method == 1){
$this->db->update_batch($this->table_test, $data , 'test_id');
}
else if($method == 2){
foreach($data as $k=>$v){
$sql = "UPDATE $db_table SET t_franchise_price = TRIM(BOTH ',' FROM CONCAT(COALESCE(t_franchise_price,''), ',', ?)) WHERE test_id = ?;";
$result = $this->db->query($sql, array($v['t_franchise_price'], $v['test_id']));
if($result === false) {
echo "ERROR at index $k - query: ((".$this->db->last_query()."))<br/><br/>\n\n";
print_r($this->db->error()); echo "<br/><br/>\n\n";
}
}
}
}
虽然我不确定。 update_batch 是否可以不替换以前的数据,下一个更新的值将以逗号分隔作为数组形式放在 SAME FILED 上。为了更好地理解,我展示了 2 个演示图像。我的代码已经 运行 仅用于批量更新但不是 update_batch 增加值 而不替换以前的数据!
例如-
对于第一次更新,列字段将在 "t_franchise_price" 列 显示如下 ---
第二次执行下一次更新时,列字段 "t_franchise_price" 将如下所示 ---
我的控制器是
public function batch_update() {
$sID = $this - > input - > post('test_id[]');
$sAmt = $this - > input - > post('test_priceUpdt[]');
$sOfficeId = $this - > input - > post('fran_office_id');
date_default_timezone_set('Asia/Kolkata');
$edited_test = array();
if (!empty($sID)) {
foreach($sID as $k => $v) {
$edited_test = array(
'test_id' => $sID[$k],
'test_priceUpdt' => $sAmt[$k],
'fran_office_id' => $sOfficeId,
'last_edit_date' => date('Y-m-d H:i:s', time())
);
$edited_test = json_encode($edited_test);
$postData[] = array('test_id' => $v, 't_franchise_price' => $edited_test);
}
$data['franchise_tst_pkg'] = (object) $postData;
}
#-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - #
if ($this - > form_validation - > run() === true) {
$this - > franchise_price_model - > batchTestupdate($postData);
$this - > session - > set_flashdata('message', display('save_successfully'));
redirect('branch/franchise_price/create');
}
**我的模特是**
public function batchTestupdate($data = [])
{
$this->db
->update_batch($this->table_test, $data , 'test_id');
}
查看
<tr>
<td>
<input type="text" name="test_name[]" class="form-control" placeholder="" value="<?php echo $subject->test_name; ?>" disabled>
<input type="hidden" name="test_id[]" class="form-control" placeholder="" value="<?php echo $subject->test_id; ?>">
</td>
<td>
<input type="text" name="test_price[]" class="form-control" placeholder="" value="<?php echo $subject->test_price; ?>" disabled>
</td>
<td>
<input type="text" name="test_priceUpdt[]" class="form-control" placeholder="" value="<?php echo $subject->test_price; ?>" id="test_priceEdt">
</td>
</tr>
追加到 table 字段 t_franchise_price
而不是覆盖。
变化:
$this - > franchise_price_model - > batchTestupdate($postData);
收件人:
$this->franchise_price_model->batchTestupdate($postData, 2);
将您的模型方法更改为:
public function batchTestupdate($data = [], $method=1){
$db_table = $this->db->dbprefix($this->table_test);
$sql = "";
if($method == 1){
$this->db->update_batch($this->table_test, $data , 'test_id');
}
else if($method == 2){
foreach($data as $k=>$v){
$sql = "UPDATE $db_table SET t_franchise_price = TRIM(BOTH ',' FROM CONCAT(COALESCE(t_franchise_price,''), ',', ?)) WHERE test_id = ?;";
$result = $this->db->query($sql, array($v['t_franchise_price'], $v['test_id']));
if($result === false) {
echo "ERROR at index $k - query: ((".$this->db->last_query()."))<br/><br/>\n\n";
print_r($this->db->error()); echo "<br/><br/>\n\n";
}
}
}
}