Xhynk
Xhynk

Reputation: 13850

Simple counting through loop php?

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.

Upvotes: 0

Views: 89

Answers (2)

Mihai Iorga
Mihai Iorga

Reputation: 39704

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;

Upvotes: 2

Gerald Versluis
Gerald Versluis

Reputation: 34013

$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.

Upvotes: 2

Related Questions