...

View Full Version : Shipping Function?



michaelespinosa
08-29-2007, 06:30 PM
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

rafiki
08-29-2007, 06:55 PM
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';

michaelespinosa
08-29-2007, 07:46 PM
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


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.

rafiki
08-29-2007, 08:06 PM
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

michaelespinosa
08-29-2007, 08:31 PM
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 (https://www.rockchurchfamily.com/promo/cdRelease.php). How do I call all the variables to the last page, because there no longer in a form.

Fumigator
08-29-2007, 09:07 PM
You should use sessions. You can get started learning about sessions on this page (http://us.php.net/manual/en/function.session-start.php) of the PHP manual.

Pennimus
08-29-2007, 09:08 PM
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.

rafiki
08-29-2007, 10:15 PM
i reccomend sessions its easier

michaelespinosa
08-29-2007, 10:27 PM
Thanks everyone for your help. It's much appreciated.

michaelespinosa
09-07-2007, 06:37 PM
In the php here:



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
round($subTotal,2) and
number_format($subTotal,2) but it kept giving me 125.00. I don't know if I was using them incorrectly or what.

michaelespinosa
09-07-2007, 06:41 PM
The desired result is 12.50

Inigoesdr
09-07-2007, 07:34 PM
number_format() works.

$ret = 12.5;
echo number_format($ret, 2); // 12.50

michaelespinosa
09-07-2007, 07:44 PM
Thanks, your right that worked. I was using it in the wrong spot.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum