Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    69
    Thanks
    11
    Thanked 2 Times in 2 Posts

    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

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,039
    Thanks
    19
    Thanked 42 Times in 42 Posts
    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'

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    69
    Thanks
    11
    Thanked 2 Times in 2 Posts
    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.
    Last edited by michaelespinosa; 08-29-2007 at 08:00 PM. Reason: updated

  • #4
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,039
    Thanks
    19
    Thanked 42 Times in 42 Posts
    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

  • #5
    New Coder
    Join Date
    Aug 2007
    Posts
    69
    Thanks
    11
    Thanked 2 Times in 2 Posts
    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.

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You should use sessions. You can get started learning about sessions on this page of the PHP manual.

  • #7
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    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.

  • #8
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,039
    Thanks
    19
    Thanked 42 Times in 42 Posts
    i reccomend sessions its easier

  • #9
    New Coder
    Join Date
    Aug 2007
    Posts
    69
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Thanks everyone for your help. It's much appreciated.

  • #10
    New Coder
    Join Date
    Aug 2007
    Posts
    69
    Thanks
    11
    Thanked 2 Times in 2 Posts
    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.

  • #11
    New Coder
    Join Date
    Aug 2007
    Posts
    69
    Thanks
    11
    Thanked 2 Times in 2 Posts
    The desired result is 12.50

  • #12
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    number_format() works.
    PHP Code:
    $ret 12.5;
    echo 
    number_format($ret2); // 12.50 

  • Users who have thanked Inigoesdr for this post:

    michaelespinosa (09-07-2007)

  • #13
    New Coder
    Join Date
    Aug 2007
    Posts
    69
    Thanks
    11
    Thanked 2 Times in 2 Posts
    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
    •