• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 54
  • Last Modified:

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

0
rgranlund
Asked:
rgranlund
1 Solution
 
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now