Author Topic: still calc problams.  (Read 2328 times)

0 Members and 1 Guest are viewing this topic.

Offline redarrowTopic starter

  • Freak!
  • Posts: 8,150
  • Gender: Male
  • PHP IS FOR LIFE!
    • View Profile
    • my free dating site (a blind php programmer!)
still calc problams.
« on: October 09, 2009, 06:13:37 AM »
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


Code: [Select]
<?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: &pound;".number_format($_SESSION['total'],2)." <br><br>";
echo " Monthly Payments: {$_SESSION['payments']} payments of &pound;".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: &pound;".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>

« Last Edit: October 09, 2009, 06:14:25 AM by redarrow »
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.ath.cx/paypal_info/paypal1_info.inc

Offline ameyemad

  • Enthusiast
  • Posts: 51
    • View Profile
Re: still calc problams.
« Reply #1 on: October 09, 2009, 06:32:43 AM »
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;
}

Offline redarrowTopic starter

  • Freak!
  • Posts: 8,150
  • Gender: Male
  • PHP IS FOR LIFE!
    • View Profile
    • my free dating site (a blind php programmer!)
Re: still calc problams.
« Reply #2 on: October 09, 2009, 06:38:22 AM »
that was even worse thanks ...


please don't alter the code as it is just correct it, short cutting dose not help cheers.
« Last Edit: October 09, 2009, 06:41:22 AM by redarrow »
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.ath.cx/paypal_info/paypal1_info.inc

Offline redarrowTopic starter

  • Freak!
  • Posts: 8,150
  • Gender: Male
  • PHP IS FOR LIFE!
    • View Profile
    • my free dating site (a blind php programmer!)
Re: still calc problams.
« Reply #3 on: October 09, 2009, 06:56:35 AM »

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

still no luck.

the code is professionally made up but not working man.

Code: [Select]
<?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;
}
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.ath.cx/paypal_info/paypal1_info.inc

Offline ameyemad

  • Enthusiast
  • Posts: 51
    • View Profile
Re: still calc problams.
« Reply #4 on: October 09, 2009, 07:45:16 AM »
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

Code: [Select]
$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;

Online KevinM1

  • Global Moderator
  • Fanatic
  • *
  • Posts: 3,911
  • Gender: Male
    • View Profile
Re: still calc problams.
« Reply #5 on: October 09, 2009, 08:44:49 AM »
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 = ...).
Don't go to w3schools.  Here's why
Every time a PHP coder uses 'global' a kitten dies.

Offline .josh

  • Administrator
  • 'Insane!'
  • *
  • Posts: 13,152
  • Grumpy Old Man
    • View Profile
Re: still calc problams.
« Reply #6 on: October 09, 2009, 09:19:39 AM »
the code is professionally made up but not working man.

uh...I thought one of the things that differentiated professional code from amateur code was the little things, like...the code working... 

Did I help you? Feeling generous? Donate to me! | Donate to phpfreaks!