Jump to content

[SOLVED] Calculating help cheers.


redarrow

Recommended Posts

Advance thank you.

 

I was creating a advance basket for a company and i am stuck.

 

not used the old maths function's for years can you help..

 

looks wrong.

<?php

$price="20.00";

$quienty="4";

$shipping="7.50";

$discount="20%";

$tax="17.50";

$payments="12";


$total=$price*$quienty;

$total=$total+$shipping;

$total=$total-$discount;


$taxrate=$tax/100;

$taxrate=$taxrate+1;

$total=$total*$taxrate;

$monthly=$total/$payments;

echo "Complete total ".£.number_format($total,2)."\n
<br> $payments monthly payments ".£.number_format($monthly,2)." ";

?>

 

result

 

Complete total £79.31 // how this correct no way?

 

12 monthly payments £6.61  // looks ok? from above result.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/177058-solved-calculating-help-cheers/
Share on other sites

Once thing I noticed

 

$total = $total - $discount

 

$discount is 20%. Can you subtract a real with a percent?

 

 

Also, that would total to about  87.5 without taxes. Whats so wrong about the numbers? Whats it supposed to be?

 

Also, everything looks fine to me besides the percent subtracting from a real.

 

EDIT: I just calculated myself and came to about 80 pounds. I don't think your code is wrong. Thats just the price. 17.5% is a hefty tax.

 

 

20 * 4 = (Price without anything)

 

80 * .8 ( .8 = 4/5 = 80% of orignal)

64 * 1.175 = 75.2 (Taxes)

75.2 + 7.50 = 81.2 (Shipping, shipping doesn't have sales tax)

 

Edit: Yeah I screwed up my statistics and modified them to be accurate.

 

80$ is price, then discount, then you add the taxes, then shipping price.

London is hefty lol, not my fault, not prime minister pal, lol...

 

 

can you show me the same in a OOP code please i love learning that stuff....

 

whale i thort ill ask on here because i am no good at php and i got it correct first time no way?

 

Here a function way, nice i think ?

 

<?php
function add_shopping($price,$quienty,$shipping,$discount,$tax,$payments){
$total=$price*$quienty;
$total=$total+$shipping;
$total=$total-$discount;
$taxrate=$tax/100;
$taxrate=$taxrate+1;
$total=$total*$taxrate;
$monthly=$total/$payments;
echo "Complete total ".£.number_format($total,2)."\n
<br> $payments monthly payments ".£.number_format($monthly,2)." ";
return;
}
echo add_shopping('20.00','10','7.50','10%','17.50','12');
?>

 

short version

<?php
function add_shopping($price,$quienty,$shipping,$discount,$tax,$payments){
$total=($price*$quienty)+($shipping)-($discount);
$taxrate=($tax/100)+($taxrate+1);
$total=$total*$taxrate;
$monthly=$total/$payments;
echo "Complete total ".£.number_format($total,2)."\n
<br> $payments monthly payments ".£.number_format($monthly,2)." ";
return;
}
echo add_shopping('20.00','1','7.50','10%','17.50','12');
?>

 

shortest i personally can go.

<?php
function add_shopping($price,$quienty,$shipping,$discount,$tax,$payments){
$total=($price*$quienty)+($shipping)-($discount);
$taxrate=($tax/100)+1;
$total=$total*$taxrate;
$monthly=$total/$payments;
echo "Complete total ".£.number_format($total,2)."\n
<br> $payments monthly payments ".£.number_format($monthly,2)." ";
return;
}
echo add_shopping('20.00','1','7.50','10%','17.50','12');
?>

Last post on this sorry.

 

I added session's so the design of the web page, can be set up correctly.

 

 

<?php session_start();
function add_shopping($price,$quienty,$shipping,$discount,$tax,$payments){
$total=($price*$quienty)+($shipping)-($discount);
$taxrate=($tax/100)+1;
$total=$total*$taxrate;
$monthly=$total/$payments;
$_SESSION['monthly']=$monthly;
$_SESSION['total']=$total;
$_SESSION['payments']=$payments;
return;
}
echo add_shopping('20.00','1','7.50','10%','17.50','12');
echo "Complete total ".£.number_format($_SESSION['total'],2)."\n
<br> {$_SESSION['payments']} monthly payments ".£.number_format($_SESSION['monthly'],2)." ";
?>

 

If you learn all this maths stuff, as i have posted you can design a grate cart script.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.