I need some help because i wondering whats wrong with my code I have an array and i just looping through the array to update some data in database,
here is the array from data_d varible:
Array
(
[0] => stdClass Object
(
[id] => 1
[jumlah] => 47500
[jenis] => 41
[anggota] => 1
[dk] => D
)
[1] => stdClass Object
(
[id] => 2
[jumlah] => 99997500
[jenis] => 40
[anggota] => 1
[dk] => D
)
[2] => stdClass Object
(
[id] => 13
[jumlah] => 997500
[jenis] => 32
[anggota] => 2
[dk] => D
)
[3] => stdClass Object
(
[id] => 14
[jumlah] => 4000000
[jenis] => 40
[anggota] => 1
[dk] => D
)
)
Here is the code :
function jumlahtabungan()
{
$conf_bunga = $this->bunga_m->get_key_val();
$adm_sp = $conf_bunga['adm_sp'];
$data_d = $this->simpanan_m->jml_simpan_anggota_d();
$lookup = [];
foreach ($data_d as &$object) {
$key = $object->jenis . ',' . $object->anggota;
if (!isset($lookup[$key])) {
$object->jumlah -= $adm_sp;
$lookup[$key] = true;
}
}
// echo "<pre>";
// print_r($data_d);
// echo "</pre>";
foreach ($data_d as $x) {
$total = $x->jumlah;
// var_dump($total);
$simpan_arr = array(
'jumlah' => $total,
);
$this->db->where('id', $x->id);
return $this->db->update('tbl_trans_sp', $simpan_arr);
}
}
as you can see in last loop i try to update the database with array results, but just one row getting update, i expecting atleast 3 rows. I appreciate any help, Thank you.
CodePudding user response:
Putting this here just so that others who might come across this question will find the answer to the problem easier, and understand it. Do also note that this has to do with PHP, not codeigniter. (Tags)
Using return will end your current function, and has the option to also bring a value that was created inside said function. In this specific case, your use of return causes your foreach to stop during its first iteration. Simply put your return outside the foreach if you want the foreach to run more than one iteration.
So, in essance, put this row outside of the foreach brace.
return $this->db->update('tbl_trans_sp', $simpan_arr);
Read more about return here.
CodePudding user response:
return should outside in foreach