I have to update the table daytot
with the values present in the array named $result3
.
How could this be done?
$this->db->select('tdate');
$this->db->where('tdate >=', $newDate);
$this->db->where('tdate <=', $date);
$result3=$this->db->get('daytot')->result_array();
$amount=$this->input->post('credit1');
for($i=0;$i<count($result3);$i++) {
$opcash=$result3[$i]['opcash']+$amount;
$data1 = array(
'tdate'=>$newDate,
'total_credit'=>$total_credit['amount'],
'total_debit'=>$total_debit['amount'],
'opcash'=>$opcash,
;
$this->db->where('tdate', $result3[$i]);
$this->db->update('daytot', $data1);
}
Try to use foreach
loop for update the record
$result3
array structure is
{ ["tdate"]=> string(10) "24/12/2018" ["opcash"]=> string(3) "500" } { ["tdate"]=> string(10) "25/12/2018" ["opcash"]=> string(3) "1000" }
So try like this. And make sure you have all the variable values
$this->db->select('tdate');
$this->db->where('tdate >=', $newDate);
$this->db->where('tdate <=', $date);
$result3=$this->db->get('daytot')->result_array();
$amount=$this->input->post('credit1');
Now check if $result3
have record or not
if(isset($result3) && count($result3) > 0){
foreach($result3 as $result) {
$opcash=$result['opcash']+$amount;
$data1 = array(
'tdate'=>$newDate,
'total_credit'=>$total_credit['amount'],
'total_debit'=>$total_debit['amount'],
'opcash'=>$opcash,
;
$this->db->where('tdate', $result['tdate']);
$this->db->update('daytot', $data1);
}
}
Try below method. id
is a unique value from the table daytot
, you can use any unique field.
$this->db->select('id, tdate, opcash'); //id => primary key
$this->db->where('tdate >=', $newDate);
$this->db->where('tdate <=', $date);
$result3 = $this->db->get('daytot')->result_array();
$amount = $this->input->post('credit1');
Using For
$data1 = array();
for ($i = 0; $i < count($result3); $i++) {
$opcash = $result3[$i]['opcash'] + $amount;
$data1[] = array(
'id' => $result3[$i]['id'],
'tdate' => $newDate,
'total_credit' => $total_credit['amount'],
'total_debit' => $total_debit['amount'],
'opcash' => $opcash
);
}
$this->db->update_batch('daytot', $data1, 'id');
OR Using Foreach
foreach($result3 as &$data){
$opcash = $data['opcash'] + $amount;
$data['tdate'] = $newDate;
$data['total_credit'] = $total_credit['amount'];
$data['total_debit'] = $total_debit['amount'];
$data['opcash'] = $opcash;
}
$this->db->update_batch('daytot', $result3, 'id');
First you need to select opcash
column too :
$this->db->select('tdate,opcash');
Then apply an index 0
on each foreach
iteration :
if(isset($result3) && count($result3) > 0){
foreach($result3 as $r) {
$opcash=$r[0]['opcash']+$amount;
$data1 = array(
'tdate'=>$newDate,
'total_credit'=>$total_credit['amount'],
'total_debit'=>$total_debit['amount'],
'opcash'=>$opcash,
);
$this->db->where('tdate', $r[0]['tdate']);
$this->db->update('daytot', $data1);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.