/    Sign up×
Community /Pin to ProfileBookmark

Values in the loop

hello, got this annoying issue with the sum of values, I am displaying all the DepositCost in the selected date, year… grouped by months

January
February
….
December

so I am adding them together using SUM, but I dont want to add the value when commission is 0 and not quite sure how to do it as its always taking the first record and applying changes to others, here it is mysql query and below php code

mysql_select_db($database_franchisecms, $franchisecms);
$query_enquires = sprintf(“SELECT MONTH(TrailDate), commission, SUM(DepositCost), SUM(DepositCost1) FROM appointment WHERE YEAR(TrailDate) = ‘2011’ GROUP BY YEAR(TrailDate), MONTH(TrailDate)”);
$enquires = mysql_query($query_enquires, $franchisecms) or die(mysql_error());
$row_enquires = mysql_fetch_assoc($enquires);
$totalRows_enquires = mysql_num_rows($enquires);

[code=html]<?php do { ?>

<?php
if ($row_enquires[‘commission’] != 0) {

$commission= 75;

} else {

$commission= 20;
}

?>
<?php
// here I want to add only commission when its exist
echo $deposit = $row_enquires(‘SUM(DepositCost)’) + $commission;

// here I want to add only commission when its non exist
echo $deposit1 = $row_enquires(‘SUM(DepositCost1)’) + $commission;
?>
<?php } while ($row_enquires = mysql_fetch_assoc($enquires)); ?>[/code]

I don’t know is that make any sense but I can explain in more details. Thank you

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 22.2011 — Afraid I'm still confused, though I suspect you may be able to do it in the query by adding an IF() function, something like:
<i>
</i>SELECT
MONTH(TrailDate),
SUM(DepositCost + IF(commission = 0, 20, 75)) AS cost_plus_com,
SUM(DepositCost1 + IF(commission = 0, 20, 75)) AS cost_plus_com_1
FROM appointment
WHERE YEAR(TrailDate) = '2011'
GROUP BY YEAR(TrailDate), MONTH(TrailDate)

But like I said, I'm pretty sure I'm not entirely following what the logic is you want to implement, so only take the above as a general example of using IF() within the query.
×

Success!

Help @spacemoc 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...