Jump to content

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;

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.