Jump to content

Recommended Posts

i got calculation problems man ...

 

i am very sure that the shipping fee not adding up or some think..

 

test url.

http://simpleforum.ath.cx/add_up.php

 

 

<?php session_start();

if(isset($_POST['submit'])){


if(empty($_POST['tax']) || empty($_POST['shipping']) || empty($_POST['price'])){

	echo"Stop pressing the button on it own!";

	exit;
}

add_it_all_up($_POST['price'],$_POST['quienty'],$_POST['shipping'],$_POST['discount'],$_POST['tax'],$_POST['payments']);

echo " Total price: £".number_format($_SESSION['total'],2)." <br><br>";
echo " Monthly Payments: {$_SESSION['payments']} payments of £".number_format($_SESSION['monthly'],2)." <br><br>";
echo " Number of payments:{$_SESSION['payments']} <br><br>";
echo " Current tax rate: {$_SESSION['taxrate']}%    <br><br>";
echo " Current discount: {$_SESSION['discount']}%  <br><br>";
echo " Shipping charge: £".number_format($_SESSION['shipping'],2)."  <br><br>";
}


function add_it_all_up($price,$quienty,$shipping,$discount,$tax,$payments){

$total=$price * $quienty;
$total=$total - $discount;
$total=$total + $shipping;
$taxrate=$tax / 100;
$taxrate=$taxrate + 1;
$total=$total * $taxrate;
$monthly=$total / $payments;
$_SESSION['payments']=$payments;
$_SESSION['monthly']=$monthly;
$_SESSION['total']=$total;
$_SESSION['taxrate']=$tax;
$_SESSION['discount']=$discount;
$_SESSION['shipping']=$shipping;

return;
}
?>

<html>
<head>
<title>shopping test via tax</title>
</head>

<body>

<center>

<form method="POST">
PRICE (Example 20.00))
<br>
<input type="text" name="price" MAXLENGTH='5' SIZE='5'>
<br><br>
QUIENTY((Example 1 - 20 object's))
<br>
<select name="quienty">
<?php for($i=1; $i<21; $i++){echo "<option value='$i'>$i</option>";}?>
</select>
<br><br>
SHIPPING PRICE ((Example: 7.50))
<br>
<input type="text" name="shipping" maxlenth='4' size='4'>
<br><br>
DISCOUNT ((Example persentage discount %))
<br>
<select name="discount">
<?php 
$dis=array(10,20,30,40,50,60,70,80,90,100);
for($d=0; $d<count($dis); $d++){echo "<option value='{$dis[$d]}'>{$dis[$d]}</option>";}?>
</select>
<br><br>
TAX RATE ((Example: 17.50))
<br>
<input type="text" name="tax" MAXLENGTH='5' SIZE='5'>
<br><br>
MONTHLY PAYMENTS ((Example 1- 12 months))
<br>
<select name="payments">
<?php for($m=1; $m<13; $m++){echo "<option value='$m'>$m</option>";}?>
</select>
<br><br>
<input type="submit" name="submit" value="Total">
</form>
</center>

</body>
</html>

 

 

Link to comment
https://forums.phpfreaks.com/topic/177071-still-calc-problams/
Share on other sites

you might want to try...

 

 

function add_it_all_up($price,$quienty,$shipping,$discount,$tax,$payments){

$total=$price * $quienty;

 

$_DISCOUNT = $total - (($total / 100) * $discount);

$_TAXRATE = $total + (($total / 100) * $taxrate);

 

  $total=$total - $_DISCOUNT;

  $total=$total + $shipping;

 

  $monthly=$total / $payments;

 

  $_SESSION['payments']=$payments;

  $_SESSION['monthly']=$monthly;

  $_SESSION['total']=$total;

  $_SESSION['taxrate']=$tax;

  $_SESSION['discount']=$_DISCOUNT;

  $_SESSION['shipping']=$shipping;

 

  return;

}

Link to comment
https://forums.phpfreaks.com/topic/177071-still-calc-problams/#findComment-933631
Share on other sites

 

Even changed the order, of the adding, and multiplying and dividing.

 

still no luck.

 

the code is professionally made up but not working man.

 

<?php

function add_it_all_up($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;
$_SESSION['payments']=$payments;
$_SESSION['monthly']=$monthly;
$_SESSION['total']=$total;
$_SESSION['taxrate']=$tax;
$_SESSION['discount']=$discount;
$_SESSION['shipping']=$shipping;

return;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/177071-still-calc-problams/#findComment-933651
Share on other sites

well, i think you need to explain more. when I tried your demo, your discount came up as a percentage, but in your code you have $total=$total - $discount; which is just removing the amount entered in form, it's not removing a percentage of total.

 

again with taxrate, you have divided by 100, so if I entered 17.50 in box, it would become 1.75, then you add 1, so it would become 2.75. It is incorrect. Then you are multiplying total by 2.75.

 

So if I start with these in your form:

PRICE 20.00

QUIENTY 1

SHIPPING PRICE 10.00

DISCOUNT 10

TAX RATE 10

MONTHLY PAYMENTS 1

 

It SHOULD equal ...

PRICE x QUIENTY = 20.00

PRICE + SHIPPING = 30.00

then DISCOUNT 10% would equal 27.00

then TAX 10% would be 29.70

 

$total=$price * $quienty;//equal 20.00
$total=$total + $shipping;//equal 30.00
$_DISCOUNT = $total - (($total / 100) * $discount);//equal 27.00
$total=$total - $_DISCOUNT;
$_TAXRATE = $total + (($total / 100) * $taxrate);//equal 29.70
$total=$total + $_TAXRATE;
$monthly=$total / $payments;

Link to comment
https://forums.phpfreaks.com/topic/177071-still-calc-problams/#findComment-933673
Share on other sites

It's a scope problem.  You perform all that math, but don't actually return $total.  You need to do:

 

function add_it_all_up(/* argument list */)
{
   //math here

   return $total;
}

 

Then, in the main code, assign that to another variable:

 

$final_price = add_it_all_up(/* argument list */);

 

Just putting 'return;' doesn't do anything.  It merely exits the function.  To return a value from the function, you need to explicitly return it in the function itself (return $total;) and, in the code that actually invokes the function, assign it to a variable for future use ($final_price = ...).

Link to comment
https://forums.phpfreaks.com/topic/177071-still-calc-problams/#findComment-933694
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.