I have some lines of data on a table as shown below :
+-------------------+-------+
| criteria | value |
+-------------------+-------+
| Pengalaman Kerja | 4 |
| Pendidikan | 3 |
| Usia | 5 |
| Status Perkawinan | 2 |
| Alamat | 6 |
| Pengalaman Kerja | 3 |
| Pendidikan | 1 |
| Usia | 4 |
| Status Perkawinan | 2 |
| Alamat | 1 |
+-------------------+-------+
10 rows in set (0.00 sec)
I tried to fetch them all in PHP using fetch_object() and then convert the returned data into arrays and sum the values of each criteria listed on the table.
example : Pengalaman kerja has two values (4 and 3) so the result for Pengalaman Kerja array will be 7.
$result = $db->query($sql);
$value = array();
while($row=$result->fetch_object()){
if(!isset($value[$row->criteria]){
$value[$row->criteria] = 0;
})
$value[$row->criteria] += $row->value;
var_dump($value);
}
When I ran var_dump to $value (which is now contains summed value of each criteria) it showed me :
array(5) {
["Pengalaman Kerja"]=>
int(7)
["Pendidikan"]=>
int(4)
["Usia"]=>
int(9)
["Status Perkawinan"]=>
int(4)
["Alamat"]=>
int(6)
}
Pengalaman kerja is correct because 4 + 3 = 7
Pendidikan is correct because 3 + 1 = 4
Usia is correct because 5 + 4 = 9
Status Perkawinan is correct because 2 + 2 = 4
And the wrong thing is Alamat showing 6 that it should be 7 because 6 + 1 = 7
I'm sure it was because one of Alamat record was on the last of lines and the while() function didn't count until the last of lines.
While() function only counted to the last - 1
How can I solve this problem so Alamat shows 7 as it is suppossed to show ?