Nick
Nick

Reputation: 382

Create an array and then add everything together

I'm trying to export information from ExpressionEngine CMS into an array and then add the contents of the array together, I'm pretty sure I'm nearly there but have been battling with the code for an hour!

Here's the code, it's selecting the prize_points column from my table where the member id is that of the current logged in user. It's then pulling out the prize_points associated with that member into an array, I just can't work out how to add them all together at the end.

$query = ee()->db->query("SELECT prize_points FROM exp_rmdy_member_prize_data WHERE member_id = '{member_id}'");

if ($query->num_rows() > 0)
{
    foreach($query->result_array() as $row)
    {
      // define claimed_total
      $current_prize_points = $row['prize_points'];
      $subtotal_prize_points = $current_prize_points + $row['prize_points'];
    }
    echo $subtotal_prize_points;

}

Upvotes: 0

Views: 60

Answers (2)

Norman
Norman

Reputation: 79

My Guess ,You trying to echo $subtotal_prize_points and you got Nothing

Please refer to the http://php.net/manual/en/language.variables.scope.php

if you define something in the for-each loop, you will not able to use it "Outside" of the loop. you have to define the variable outside the loop first then modify the value of it.

Upvotes: 0

Bloafer
Bloafer

Reputation: 1326

It looks like you are trying to do this:

$subtotal_prize_points = 0;
$query = ee()->db->query("SELECT prize_points FROM exp_rmdy_member_prize_data WHERE member_id = '{member_id}'");
if($query->num_rows() > 0){
    foreach($query->result_array() as $row){
      // define claimed_total
      $subtotal_prize_points += $row['prize_points'];
    }
}
echo $subtotal_prize_points;

Upvotes: 1

Related Questions