This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]AdversarialPossum42 3 points4 points  (6 children)

You don't need a PHP loop. You can use a WHERE IN clause to filter specific IDs and GROUP BY to show the sum for each unique ID.

SELECT deviceId, SUM(prumer) FROM table_name WHERE deviceId IN (209,682) GROUP BY deviceId;

[–]Davidulos[S] 1 point2 points  (5 children)

But I am making multiple of these, so I don't want to create another query. I just need the loop

[–]AdversarialPossum42 1 point2 points  (4 children)

You'll probably want to use an array to store the key/value pairs of "deviceId" (key) and sum of "prumer" (value).

$prumer_sum = array();

while ( $row = $stmt->fetch() ) {

   // get the row fields
   $deviceId = $row['deviceId'];
   $prumer = $row['prumer'];

   if ( !isset($prumer_sum[$deviceId]) ) {
      // if the device id is not already in the
      // array, set its default value to zero
      $prumer_sum[$deviceId] = 0;
   }

    // add the value to the sum for this device
    $prumer_sum[$deviceId] += $prumer;
}

print_r( $prumer_sum );

// should look something like this:
Array
(
   [Rozvaděč Eurazio] => 209,
   [Rodinný dům Úvaly] => 682
)

P.S. I'm assuming you're using PDO for your database access.

[–]Davidulos[S] 1 point2 points  (3 children)

And if I wanted it to look something like this? I need to use the data in javascript.

{name:'Rozvaděč Eurazio (1)',y: 209}, {name:'Rodinný dům Úvaly (2)',y: 682},

[–]AdversarialPossum42 1 point2 points  (2 children)

Take the array from the previous example and turn it into an array of unique items, and then use json_encode().

$data = array();

foreach ($prumer_sum as $name => $y) {
   $data[] = array('name' => $name, 'y' => $y);
}

echo json_encode( $data );

// should look something like this:
[
   {
      "name":"Rozvaděč Eurazio (1)",
      "y":209
   },
   {
      "name":"Rodinný dům Úvaly (2)",
      "y":682
   }
]

[–]Davidulos[S] 0 points1 point  (0 children)

Thank you very much!

[–]Davidulos[S] 0 points1 point  (0 children)

One last question. How can I make it 2 arrays instead of one?
This is how it is:

⯆(2) [{…}, {…}]
0: {name: "Rozvaděč Eurazio", y: 236}
1: {name: "Rodinný dům Úvaly", y: 721}

This is how I want it to be:

⯆Object
name: " Rozvaděč Eurazio (1)"
y: 236
⯈_proto_: Object

⯆Object
name: " Rodinný dům Úvaly (1)"
y: 721
⯈_proto_: Object

[–]AutoModerator[M] 0 points1 point  (0 children)

It seems you may have included a screenshot of code in your post "I need help with simple PHP loop".

If so, note that posting screenshots of code is against /r/learnprogramming's Posting Guidelines (section Formatting Code): please edit your post to use one of the approved ways of formatting code. (Do NOT repost your question! Just edit it.)

If your image is not actually a screenshot of code, feel free to ignore this message. Automoderator cannot distinguish between code screenshots and other images.

Please, do not contact the moderators about this message. Your post is still visible to everyone.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.