-1

I have the following code and $totalDonations should output 1500.

<?php
    $donations = $wpdb->get_results("
      SELECT amount
      FROM wp_fullstripe_payments
    ");
?>

<?php $totalDonations = array_sum($donations); ?>

<?php echo $totalDonations; ?>

I get 0 from $totalDonations.

Here is the var_dump for $donations:

array(2) { [0]=> object(stdClass)#2108 (1) { ["amount"]=> string(3) "500" } [1]=> object(stdClass)#2107 (1) { ["amount"]=> string(4) "1000" } }

This doesn't work either:

<?php echo $donations[0]; ?>
NPE
  • 486,780
  • 108
  • 951
  • 1,012
Alexnl
  • 397
  • 1
  • 3
  • 12

1 Answers1

1

You are getting the result as an stdClass

Try

<?php 
     $totalDonations;
     foreach ($donation as $donations){
       totalDonations += $donation->amount;
     } 
?>

Check out this link : http://www.webmaster-source.com/2009/08/20/php-stdclass-storing-data-object-instead-array/

Jay S.
  • 1,318
  • 11
  • 29