/    Sign up×
Community /Pin to ProfileBookmark

Adding variables values in php

hi guys.. need a bit of a help here.. ?
well.. first of.. ill show u the code that im workin on..

so im extracting values from a db..

[code=php] while ($row = $db->fetch()) {
$amount1 = $row[amount1];
$amount2 = $row[amount2];
$totalAmount = $amount1 + $amount2;

echo $totalAmount;
}

echo $grandTotal;[/code]

and then at the very end, after looping through all the data, i wanted to show a grand total ($grandTotal)… which is the total of $totalAmount…

so how can i do this? any suggestions? thank you..

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 04.2006 — [code=php]
$grandTotal = 0;
while ($row = $db->fetch()) {
$amount1 = $row[amount1];
$amount2 = $row[amount2];
$totalAmount = $amount1 + $amount2;
$grandTotal += $totalAmount;
echo $totalAmount;
}

echo $grandTotal;
[/code]
Copy linkTweet thisAlerts:
@stanleynsauthorJul 04.2006 — woah.. ?

worked like a charm..

thx nogdog
Copy linkTweet thisAlerts:
@aussie_girlJul 04.2006 — What about..

[code=php]$query = "SELECT SUM(amount1 + amount2) FROM tablename";
$grandtotal = mysql_query($query);
echo $grandtotal;[/code]
×

Success!

Help @stanleyns spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...