rolandmy
asked on
counting total
I have the code below working. I need the total for $finalcharge as in total charges. How do I display the total amount charged with $finalcharge? I am just displaying all the result in one page where adding a total amount charged would be ideal.
if ($calltype!='4'){
$finalcharge = $actualcharge*$price;
<tr><td>xxx</td><td>".$fin alcharge." </td></tr> \n");
}
if ($calltype!='4'){
$finalcharge = $actualcharge*$price;
<tr><td>xxx</td><td>".$fin
}
Can you clarify what you need. Can't see what you want from the code you've posted, as the code you've posted doesn't seem valid (you seem to be missing the 'echo' part).
If you want a running total, you can do something like:
If you want a running total, you can do something like:
$total = 0;
//start of a loop
$finalcharge = $actualcharge*$price;
echo $finalcharge;
$total += $finalcharge;
//finish loop
echo $total;
ASKER
I need to display the total amount for $finalcharge. It is at the moment displaying line by line with individual charges.
Code as below:
while($row = mysqli_fetch_array($result ))
{
$calledpartynumber=$row['C alledParty Number'];
$durationtime=$row['Durati onTime'];
$answer = substr($calledpartynumber, 0, 5);
$answer2 = substr($answer, 0, 1);
$billing = substr($answer, 0, 2);
if ($billing=='01') {
$price='0.82';
} elseif ($billing=='02') {
$price='0.09';
} elseif ($billing=='00') {
if ($answer=='00818') {
$price='0.99';
} elseif ($answer=='00852') {
$price='0.88';
}
}
$charge = $durationtime/30;
if ( preg_match("/\./", $charge) ) {
$actualcharge = intval($charge) + 1;
}else{
$actualcharge = intval($charge) + 0;
}
if ($calltype!='4' && $durationtime!='00' && $answer2!='1'){
if ($callingpartynumber=='223 3'){
$finalcharge = $actualcharge*$price;
print("<tr><td>$durationti me</td><td >".$finalc harge."</t d></tr>\n" );
}
}
}
mysqli_close($con);
Code as below:
while($row = mysqli_fetch_array($result
{
$calledpartynumber=$row['C
$durationtime=$row['Durati
$answer = substr($calledpartynumber,
$answer2 = substr($answer, 0, 1);
$billing = substr($answer, 0, 2);
if ($billing=='01') {
$price='0.82';
} elseif ($billing=='02') {
$price='0.09';
} elseif ($billing=='00') {
if ($answer=='00818') {
$price='0.99';
} elseif ($answer=='00852') {
$price='0.88';
}
}
$charge = $durationtime/30;
if ( preg_match("/\./", $charge) ) {
$actualcharge = intval($charge) + 1;
}else{
$actualcharge = intval($charge) + 0;
}
if ($calltype!='4' && $durationtime!='00' && $answer2!='1'){
if ($callingpartynumber=='223
$finalcharge = $actualcharge*$price;
print("<tr><td>$durationti
}
}
}
mysqli_close($con);
I don't really know for sure what you are asking but I suspect this might be along the lines of what you are looking for
while($row = mysqli_fetch_array($result))
{
$calledpartynumber=$row['CalledPartyNumber'];
$durationtime=$row['DurationTime'];
$answer = substr($calledpartynumber, 0, 5);
$answer2 = substr($answer, 0, 1);
$billing = substr($answer, 0, 2);
if ($billing=='01') {
$price='0.82';
} elseif ($billing=='02') {
$price='0.09';
} elseif ($billing=='00') {
if ($answer=='00818') {
$price='0.99';
} elseif ($answer=='00852') {
$price='0.88';
}
}
$charge = $durationtime/30;
// You are dealing with this in another question so won't fix here.
if ( preg_match("/\./", $charge) ) {
$actualcharge = intval($charge) + 1;
}
/* THIS IS SUPERFLUOUS - YOU CAN REMOVE IT
else{
$actualcharge = intval($charge) + 0;
}
*/
if ($calltype!='4' && $durationtime!='00' && $answer2!='1'){
if ($callingpartynumber=='2233'){
$finalcharge += $actualcharge*$price;
}
}
}
print("<tr><td>$durationtime</td><td>".$finalcharge."</td></tr>\n");
mysqli_close($con);
Have a look at this. Declare a variable to hold the total before the loop starts, add the relevant values within the loop, and then echo out the total after the loop:
<?php
$totalCharge = 0;
while($row = mysqli_fetch_array($result)) {
$calledpartynumber=$row['CalledPartyNumber'];
$durationtime=$row['DurationTime'];
$answer = substr($calledpartynumber, 0, 5);
$answer2 = substr($answer, 0, 1);
$billing = substr($answer, 0, 2);
$finalcharge = 0;
if ($billing=='01') {
$price='0.82';
} elseif ($billing=='02') {
$price='0.09';
} elseif ($billing=='00') {
if ($answer=='00818') {
$price='0.99';
} elseif ($answer=='00852') {
$price='0.88';
}
}
$actualcharge = ceil($durationtime/30);
if ($calltype!='4' && $durationtime!='00' && $answer2!='1'){
if ($callingpartynumber=='2233'){
$finalcharge = $actualcharge*$price;
}
}
$totalCharge += $finalcharge;
}
printf("<p>%s</p>", $totalCharge);
mysqli_close($con);
?>
ASKER
Currently the display are like below:
| Duration | Charges |
| 37 | 0.18 |
| 20 | 0.09 |
| etc...
How can I get the total amount for Charges which value is derived from $finalcharge
| Duration | Charges |
| 37 | 0.18 |
| 20 | 0.09 |
| etc...
How can I get the total amount for Charges which value is derived from $finalcharge
Already given you the answer - read through my previous post.
You might want to change line 39 of my previous comment to the following:
printf("<tr><td>Total</td><td>%s</td></tr>", $totalCharge);
ASKER
@Chris - nice cleanup on the code there but is there not a potential logic bomb here?
if ($calltype!='4' && $durationtime!='00' && $answer2!='1'){
if ($callingpartynumber=='2233'){
$finalcharge = $actualcharge*$price;
}
}
$totalCharge += $finalcharge;
The original post seems to indicate that $finalcharge is only calculated when callingpartynumber is 2233. In your solution though it is increasing totalcharge each time it loops irrespective.
@julian - earlier in my code (inside the loop) the $finalcharge value is reset to 0, so although it does add it each time, unless the logic matches, it simply adds 0 - that was to address the logic problems that currently exist throughout the code - i.e. finalcharge not existing at all unless the if statement fired)
@rolandmy - check your code again - case matters, so make sure it matches:
$totalCharge is not the same as $totalcharge
$totalCharge is not the same as $totalcharge
ASKER
Updated the case.
The $totalCharge result shows 2293.83 whereas the actual total doesn't even reach 200.
I have re-attached the full code.
sample.php
The $totalCharge result shows 2293.83 whereas the actual total doesn't even reach 200.
I have re-attached the full code.
sample.php
Add the following to the very top of your code, and re-run it - it will show you any errors you have (I'm guessing there will be some).
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1)
?>
You do have a strange approach to some of your logic that may or may not fail depending on the data coming out of your database.
ASKER
Initially have a undefined variable $finalcharge at the bottom. I moved it up and there's no error anymore.
The $totalCharge result is now 762.93 where it should be less than 200.
I have also attached a sample file.
Please help. Going to sleep at this time. Will check again in 6 hours.
sample.php
The $totalCharge result is now 762.93 where it should be less than 200.
I have also attached a sample file.
Please help. Going to sleep at this time. Will check again in 6 hours.
sample.php
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I followed the code exactly, it's much better. However, the $totalCharge remains wrong. It's only a slight difference though. Could be human error. I will recalculate with a calculator and update here again.
The $rowCharges are correct though.
Edit: It's human error. Thanks for the solution.
Will read up on Ray's comment.
The $rowCharges are correct though.
Edit: It's human error. Thanks for the solution.
Will read up on Ray's comment.
The code presented does not appear to be complete. Could you send a full example of the code you are using and more clearly state what you require?
Thanks