PHP to sum a loop of numbers

What am I doing wrong?  I have a query that returns three numbers. 10, 20 and 30.  I want to add all of them up and echo out 60, but at the moment it is echoing out 10, 20, 30
<?php
     $meta_value = $wpdb->get_results("SELECT meta_value AS value FROM wp_connections_meta WHERE entry_id = '$tax_id'");
$sum = 0;
     foreach($meta_value as $row) {
    $value = $row->value;
    $sum = $sum + $value;
}
    echo $sum.'<br /><br />';

Open in new window

LVL 8
rgranlundAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim RiddlesPrepress/OMS SpecialistCommented:
I don't know how you would be getting that result with the above code.
Try running the following code, and tell me your result:
<?php
$nums = [10, 20, 30];
$sum = 0;
foreach($nums as $num) {
  $sum += $num;
}
echo $sum;
?>

Open in new window

It should output 60.
1
rgranlundAuthor Commented:
Actually, I should put all of the code because that query is in a loop. In addidtion can I put the numers in an array?

            global $wpdb;
$get_id = $wpdb->get_results('SELECT term_id AS id, name AS name FROM wp_connections_terms WHERE term_id != 1');
foreach($get_id as $row) {
    $name = $row->name;
    $id = $row->id;
    echo 'Name: '.$row->name.'&nbsp;&nbsp;';
    
       $taxid = $wpdb->get_results("SELECT entry_id AS id FROM wp_connections_term_relationships WHERE term_taxonomy_id = '$id'");
foreach($taxid as $row) {
    $tax_id = $row->id;
    //echo $tax_id.'<br />';
    
     $meta_value = $wpdb->get_results("SELECT meta_value AS value FROM wp_connections_meta WHERE entry_id = '$tax_id'");
$sum = 0;
     foreach($meta_value as $row) {
    $value = $row->value;
    $sum = $sum +$value;
}
    echo $sum.'<br /><br />';

}
}

Open in new window

0
mohan singhWeb developerCommented:
Use this code
$sum = 0;
foreach($group as $key=>$value)
{
   $sum+= $value;
}
echo $sum;

Open in new window

0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Jim RiddlesPrepress/OMS SpecialistCommented:
Try changing lines 15-18 in your code to the following:
foreach($meta_value as $row) {
  $sum = parseInt($row->value);
}

Open in new window

0
deepak singhPhp Web Developer Cum DesignerCommented:
you can use like this

$sum = 0;
foreach($meta_value as $key=>$value)
{
   $sum+= $value;
}
echo $sum;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Can I ask if you want the sum of those values why you don't just use the Database to get it In other words use a query running of a join of the three tables doing a sum of the relevant column grouping by the term and entry values. You run the query and the database gives you back your results already calculated.

NB: Not tested but the principle should be sound
SELECT sum(cm.value), ct.term_id, ctm.entry_id
FROM wp_connections_meta cm
 LEFT JOIN
     wp_connections_term_relationships ctm ON cm.entry_id = ctm.entry_id
 LEFT JOIN
     wp_connections_terms ct ON ctm.term_taxonomy_id = ct.term_id
WHERE
  ct.term_id != 1
GROUP BY ct.term_id, ctm.entry_id

Open in new window


That will give you the sum relative to each term_id / entry_id from the first two tables.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.