I have a test for my final course. I made some function but it stuck on sum of value array.
I call the data from MySQL
$stmt = $conn->prepare("SELECT r.id,id_penyakit,nama_penyakit,id_puskesmas,nama_puskesmas,created_at FROM riwayat r WHERE id_puskesmas = :id_puskesmas");
$stmt->bindValue(':id_puskesmas', $page_cat);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$chartriwayat = $stmt->fetchAll();
it will produce an array like this
Array
(
[0] => Array
(
[id] => 4
[id_penyakit] => 1
[nama_penyakit] => Ayan
[id_puskesmas] => 5
[nama_puskesmas] => Puskesmas Melur
[created_at] => 2024-02-01 10:32:14
)
[1] => Array
(
[id] => 5
[id_penyakit] => 2
[nama_penyakit] => kutilan
[id_puskesmas] => 5
[nama_puskesmas] => Puskesmas Melur
[created_at] => 2024-02-01 10:32:18
)
[2] => Array
(
[id] => 6
[id_penyakit] => 3
[nama_penyakit] => Panuan
[id_puskesmas] => 5
[nama_puskesmas] => Puskesmas Melur
[created_at] => 2024-02-01 10:32:22
)
[3] => Array
(
[id] => 8
[id_penyakit] => 3
[nama_penyakit] => Panuan
[id_puskesmas] => 5
[nama_puskesmas] => Puskesmas Melur
[created_at] => 2024-01-01 10:32:22
)
[4] => Array
(
[id] => 9
[id_penyakit] => 3
[nama_penyakit] => Panuan
[id_puskesmas] => 5
[nama_puskesmas] => Puskesmas Melur
[created_at] => 2024-01-01 11:32:22
)
)
then I need the data to be put to new array, something like sum value with same "id_penyakit" and in same date month
I’ve tried this code and work to create a new array that I want
$arraychartriwayat1 = $arraychartriwayat2 = $arraychartriwayat = array();
$ArrayFuncmonth = array(
'January'=>0,
'February'=>0,
'March'=>0,
'April'=>0,
'Mei'=>0,
'June'=>0,
'July'=>0,
'August'=>0,
'September'=>0,
'October'=>0,
'November'=>0,
'December'=>0
);
foreach ($chartriwayat as $key => $value) {
$arraychartriwayat1[$value['id_penyakit']] = array(
"name" => $value['nama_penyakit'],
"data" => $ArrayFuncmonth,
);
}
foreach ($chartriwayat as $key => $value) {
$riwayatmonth = date('F', strtotime($value['created_at']));
foreach ($ArrayFuncmonth as $ke => $va) {
$month = $ke;
if($riwayatmonth==$month){
if(isset($ArrayFuncmonth[$ke])){
$ArrayFuncmonth[$ke]+=1;
}else{
$ArrayFuncmonth[$ke]+=0;
}
}
$arraychartriwayat[$value['id_penyakit']] = array(
"name" => $value['nama_penyakit'],
"data" => $ArrayFuncmonth,
);
}
}
the result of this code is like this
Array
(
[1] => Array
(
[name] => Ayan
[data] => Array
(
[January] => 0
[February] => 1
[March] => 0
[April] => 0
[Mei] => 0
[June] => 0
[July] => 0
[August] => 0
[September] => 0
[October] => 0
[November] => 0
[December] => 0
)
)
[2] => Array
(
[name] => kutilan
[data] => Array
(
[January] => 0
[February] => 2
[March] => 0
[April] => 0
[Mei] => 0
[June] => 0
[July] => 0
[August] => 0
[September] => 0
[October] => 0
[November] => 0
[December] => 0
)
)
[3] => Array
(
[name] => Panuan
[data] => Array
(
[January] => 2
[February] => 3
[March] => 0
[April] => 0
[Mei] => 0
[June] => 0
[July] => 0
[August] => 0
[September] => 0
[October] => 0
[November] => 0
[December] => 0
)
)
)
but the problem is the data always sum instead it will be different from each "id_penyakit"
the code should increment the value that has same "id_penyakit" and same "created_at" as month
What I ask is like this
Array
(
[1] => Array
(
[name] => Ayan
[data] => Array
(
[January] => 0
[February] => 1
[March] => 0
[April] => 0
[Mei] => 0
[June] => 0
[July] => 0
[August] => 0
[September] => 0
[October] => 0
[November] => 0
[December] => 0
)
)
[2] => Array
(
[name] => kutilan
[data] => Array
(
[January] => 0
[February] => 1
[March] => 0
[April] => 0
[Mei] => 0
[June] => 0
[July] => 0
[August] => 0
[September] => 0
[October] => 0
[November] => 0
[December] => 0
)
)
[3] => Array
(
[name] => Panuan
[data] => Array
(
[January] => 2
[February] => 1
[March] => 0
[April] => 0
[Mei] => 0
[June] => 0
[July] => 0
[August] => 0
[September] => 0
[October] => 0
[November] => 0
[December] => 0
)
)
)
data in "February" must be 1 as the penyakit "Panuan" which ID "3" show in $chartriwayat
please help me to finish my final course test.
2
Answers
Array
(
[1] => Array
(
[name] => Ayan
[data] => Array
(
[January] => 0
[February] => 1
[March] => 0
[April] => 0
[May] => 0
[June] => 0
[July] => 0
[August] => 0
[September] => 0
[October] => 0
[November] => 0
[December] => 0
)
)
You use the same array
$ArrayFuncmonth
for all the elements of$arraychartriwayat
. This means that each iteration of your outer loop overwrites the previous values, which is why you get incorrect sums.You don’t need two separate loops to initialise and update the data. You can do it in a single loop.