0

I've got a simple problem that I'm not quite sure how to get around using a loop to grab php variables and add them together

Let's say I've got this loop

$total = 0;
while ( $foo ='bar' );
    $amount = meta($row, 'amount');
endwhile;

$total = 'NEW AMOUNT';

my question is, how do I add amounts (anywhere from 1 to 200) together to create the bottom $total? the amount of objects from while ( $foo ='bar'); is ever growing and can be 2 total objects, or 2000.

2 Answers 2

2

add to $total and change the ; in front of the while with : to start that while:

$total = 0;
while ( $foo = 'bar' ):
    $total += meta($row, 'amount'); // assuming `meta()` is one of your functions that extract `amount` from someplace ..
endwhile;

echo $total;
Sign up to request clarification or add additional context in comments.

1 Comment

wonderful! Works great :) - PS yes, meta() is one of my functions :). Thanks!!
2
$total = 0;
while ( $foo ='bar' )
{
    $amount = meta($row, 'amount');
    $total = $total + $amount;
}

Something like this? If both $total and $amount are integers you can just add them together.

Also I assume meta() is a method which calculates an amount and returns it?

Notice how I changed the while loop (added braces), after the closing brace you should have your $total variable with the total amount in it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.