0

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 ?

1 Answer 1

1

Why even bother doing this in PHP? If you just want the sum of all rows grouped by criteria then do it with a simple MySQL query?

SELECT criteria, SUM(value) AS total FROM table GROUP BY criteria

EDIT: If you insist on doing it your way then aside from a couple of typos in your code, which would usually cause runtime errors, your code should work as expected. When I run it against a table with the exact same data I get the expected results.

BTW, PDOStatement has no method called fetch_object (this is the name of the helper function) so if you want to use a method to fetch the query results as an object for a PDOStatement you have to use the proper name which is fetchObject().

PHP

$result = $dbh->query("SELECT * FROM test");
$value = array();

while ($row = $result->fetchObject()) {
    if (!isset($value[$row->criteria])) {
        $value[$row->criteria] = 0;
    }
    $value[$row->criteria] += $row->value;
}
var_dump($value);

Results

array (size=5)
  'Pengalaman Kerja' => int 7
  'Pendidikan' => int 4
  'Usia' => int 9
  'Status Perkawinan' => int 4
  'Alamat' => int 7
Sign up to request clarification or add additional context in comments.

6 Comments

Actually, I had done using SQL Query too before asking this question. Just looking for new ways :)
Updated my answer to include further info.
could you please point me to what has been going wrong in my code? Your code written above look similar to mine. There's no difference
I've commented in my post on everything that was changed. Basically the only difference is in a typo and using fetchObject instead of fetch_object. The latter should have actually caused a runtime error.
Okay. It's solved. It was a stupid mistake. I put var_dump before $nilai_kuadrat[$row->criteria] += $row->value;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.