Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Shipping Function?

1. ## Shipping Function?

I am new to php and making a simple order form. It was easy to do multiplication and addition, but now I need to write a function that will determine shipping. I have set amounts; total = \$10-\$20 then shipping would be \$5 if total = \$21-\$40 then shipping would be \$15 and so on. How can I do this. Thanks for help

Michael

• PHP Code:
``` function get_shipping(\$total) {if (\$total <= 20) {return 5;}elseif(\$total => 20 && \$total <= 40){return 15;}elseif(\$total => 40  && \$total <= 60){return 25;}else{return 40;}} // end function definition\$ship = get_shipping(\$cart_total);echo 'it will cost'. \$ship .'to post your goods';  ```

• Thanks for the help! I really appreciate it.
Two questions:
1. I am getting a parse error of T_DOUBLE_ARROW which I believe is the => how do i fix this?

2. last else statement I want to the \$total * 10%. Do I put that in the return?

Here's what I got
PHP Code:
``` function get_shipping(\$subTotal) {      if (\$subTotal < 20) {      return 3;      }elseif(\$subTotal => 20 && \$subTotal < 30){      return 4;      }elseif(\$subTotal => 30  && \$subTotal < 40){      return 5;      }elseif(\$subTotal => 40  && \$subTotal < 50){      return 6;      }elseif(\$subTotal => 50  && \$subTotal < 75){      return 8;      }elseif(\$subTotal => 75  && \$subTotal < 100){      return 10;      }else{      \$subTotal * .01;      }   } // end function definition                                       \$shippingTotal = get_shipping();                                      \$grandTotal = \$subTotal + \$shippingTotal;  ```
Thanks again for the help.

• PHP Code:
``` function get_shipping(\$subTotal) {      if (\$subTotal < 20) {      return 3;      }elseif(\$subTotal >= 20 && \$subTotal < 30){      return 4;      }elseif(\$subTotal >= 30  && \$subTotal < 40){      return 5;      }elseif(\$subTotal >= 40  && \$subTotal < 50){      return 6;      }elseif(\$subTotal >= 50  && \$subTotal < 75){      return 8;      }elseif(\$subTotal >= 75  && \$subTotal < 100){      return 10;      }else{     return (\$subTotal / 100 * 10); //10%      }   } // end function definition                                       \$shippingTotal = get_shipping(\$subTotal);                                      \$grandTotal = \$subTotal + \$shippingTotal;  ```
try that

• That worked great. I appreciate you helping me as I learn php. One last question. I have it setup where you enter in all your information >> then click review order >> this page shows you all the info you entered and the totals >> then click place order >> which is a page that says thanks for your order and sends me an email with your information. You can see it here. How do I call all the variables to the last page, because there no longer in a form.

• You should use sessions. You can get started learning about sessions on this page of the PHP manual.

• You either need to pass the variables to the last page using GET variables in the URL or, on the middle page, hold all the information in hidden form fields so that it can be passed via a second form button as POST variables, or use sessions as above.

• i reccomend sessions its easier

• Thanks everyone for your help. It's much appreciated.

• In the php here:

PHP Code:
``` return (\$subTotal / 100 * 10); //10%  ```
Sometimes this returns a decimal like 12.5. I am using this as a dollar amount and am trying to see how I can make sure there are always two decimal places to represent a dollar amount. I tried
PHP Code:
``` round(\$subTotal,2)  ```
and
PHP Code:
``` number_format(\$subTotal,2)  ```
but it kept giving me 125.00. I don't know if I was using them incorrectly or what.

• The desired result is 12.50

• number_format() works.
PHP Code:
``` \$ret = 12.5;echo number_format(\$ret, 2); // 12.50  ```

• ## Users who have thanked Inigoesdr for this post:

michaelespinosa (09-07-2007)

• Thanks, your right that worked. I was using it in the wrong spot.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•