Jump to content

function for multiplying total cost?


crossfuse

Recommended Posts

I'm trying to make a function to add 6% tax to the total cost which the user can type in the cost of an item. The outcome is supposed to include the shipping as well (which I've done),but using variables isn't my forte. I understand the concept of them but here's what I got so far.

 

http://cpsc.maconstate.edu/students/justin.ham/cost.html

 

<?php
print("<b>Thankyou!</b><br><br><br>");
$cost = $_POST["cost"];


$cost1 = $cost + 3;
$cost2 = $cost + 4;
$cost3 = $cost + 5;
$cost4 = $cost + 6;

if ($cost < 0) {
print("Your total cost is invalid");
}
   
   elseif ($cost <= 25) {
print("Your total cost,plus shipping, is $$cost1");
}
   elseif ($cost <= 50) {
    print("Your total cost,plus shipping, is $$cost2");
}

   elseif ($cost <= 75) {
   print("Your total cost,plus shipping, is $$cost3");
} 

   elseif ($cost > 75)  {
   print("Your total cost,plus shipping, is $$cost4");
}




?>

 

do i insert a function before the 'if' statement and what exactly would i put?

Link to comment
https://forums.phpfreaks.com/topic/153794-function-for-multiplying-total-cost/
Share on other sites

echo "<b>Thankyou!</b><br><br><br>";
$cost = $_POST["cost"];
if ($cost < 0) {
  $msg = "Your total cost is invalid";
} else {
  $shipping = array(3 => 25, 4 => 50, 5 => 75, 6 => 76);
  foreach ($shipping as $k => $val) {
    if ($cost <= $val) {
      $cost = ($cost * 1.06) + $k;
      $msg = "Your total cost, plus shipping, is $$cost";
      break;
    } // end if
  } // end foreach
} // end if..else
echo $msg;

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.