Forum Moderators: coopster
I am a bit stuck with how to correctly format this if/else statement, I am still learning PHP.
I am trying to calculate the postage for a shopping cart. The state variable is stores in a session when the user logs in.
If the user is from QLD then I want to charge him a set range of postage prices based on how much the order total is, if he lives anywhere else (=! QLD) I want to charge different amount.
I came up with this, but I am sure it wont work:
[PHP]
// Calculate postage amount based on state
if ($_SESSION['state'] == QLD) {
if ($subtotal > 100) {
$PostalAmount = number_format( 5.00, 2);
}
if ($subtotal > 200) {
$PostalAmount = number_format( 10.00, 2);
}
if ($subtotal > 300) {
$PostalAmount = number_format( 15.00, 2);
}
elseif ($_SESSION['state']!= QLD) {
if ($subtotal > 100) {
$PostalAmount = number_format( 8.00, 2);
}
if ($subtotal > 200) {
$PostalAmount = number_format( 13.00, 2);
}
if ($subtotal > 300) {
$PostalAmount = number_format( 19.00, 2);
}
[/PHP]
Any help would be GREATLY appreciated.
A fix to this could be something like this:
if ($_SESSION['state'] == "QLD") { //assuming QLD is a string not a constant
if ($subtotal > 300) {
$PostalAmount = number_format( 15.00, 2);
}
else if ($subtotal > 200) {
$PostalAmount = number_format( 10.00, 2);
}
else if ($subtotal > 100) {
$PostalAmount = number_format( 5.00, 2);
}
}
else {
if ($subtotal > 300) {
$PostalAmount = number_format( 19.00, 2);
}
else if ($subtotal > 200) {
$PostalAmount = number_format( 13.00, 2);
}
else if ($subtotal > 100) {
$PostalAmount = number_format( 8.00, 2);
}
}
I think this should work. Good luck!
I have come to a bit of a hurdle with this script....
Can anyone please help?
You will see that the code below calculates the postage and insuarance based on the subtotal amount.
I have just been asked by my client to design it so that there are different insurance rates for orders totalling $0 -$5000 (amount to change every $100)
Rate for every $100 is $1.10
$100=$1.10
$200=$2.10
$300=$3.10
and so on up to $5000
Is there a way that I can get PHP to do the work for me instead of hand coding the blocks of code for every $100 jump?
Maybe a loop or using '++' ?
[PHP]
// calculate insurance rate
if ($subtotal > 100) {
$insurance = number_format( 10.50, 2);
}
elseif ($subtotal < 40) {
$insurance = number_format( 3.50, 2);
}
elseif ($subtotal > 40) {
$insurance = number_format( 6.00, 2);
}
// calculuate postage rate
if ($_SESSION['state'] == "QLD") {
if ($subtotal > 300) {
$postage = number_format( 15.00, 2);
}
else if ($subtotal > 200) {
$postage = number_format( 10.00, 2);
}
else if ($subtotal > 100) {
$postage = number_format( 5.00, 2);
}
} else {
if ($subtotal > 300) {
$postage = number_format( 19.00, 2);
}
else if ($subtotal > 200) {
$postage = number_format( 13.00, 2);
}
else if ($subtotal > 100) {
$postage = number_format( 8.00, 2);
}
} // end of calculuate postage rate
{/PHP]
floor($price/100)*1.1 Incidentally you will probably find it easier to keep track if you change the way you show your brackets.
Instead of:
if ($_SESSION['state'] == QLD) {
if ($subtotal > 100) {
$PostalAmount = number_format( 5.00, 2);
}
Write:
if ($_SESSION['state'] == QLD)
``{
``if ($subtotal > 100)
````{
````$PostalAmount = number_format( 5.00, 2);
````}
You should be able to visually see that there is a missing } in the code above, whereas using your style it's difficult to keep track.
(Ignore the ` - they are replacing the spaces this forum can't show)
If the order value is in the variable $order then the insurance can be found as :
$insurance=ceil($order/100)*1.1;
To output the insurance then you could write:
print "Insurance cost is: $insurance";
To find the total then you could:
$total=$postage+$insurance;
What is the correct format to use for the '<' and '>' statements for my postage, i think I have it wrong.
* if subtotal is under $200 post is free
[PHP]
// calculuate postage rate
if ($_SESSION['state'] == "QLD") {
if ($subtotal > 3000) {
$postage = number_format( 15.00, 2);
}
else if ($subtotal > 1500) {
$postage = number_format( 13.00, 2);
}
else if ($subtotal > 500) {
$postage = number_format( 10.00, 2);
}
else if ($subtotal > 200) {
$postage = number_format( 7.00, 2);
}
else if ($subtotal < 200) {
$postage = number_format( 0.00, 2);
}
} else {
if ($subtotal > 3000) {
$postage = number_format( 20.00, 2);
}
else if ($subtotal > 1500) {
$postage = number_format( 17.00, 2);
}
else if ($subtotal > 500) {
$postage = number_format( 12.00, 2);
}
else if ($subtotal > 200) {
$postage = number_format( 7.00, 2);
}
else if ($subtotal < 200) {
$postage = number_format( 0.00, 2);
}
[/PHP
if ($subtotal > 3000) {
$postage = number_format( 20.00, 2);
}
else if ($subtotal > 1500) {
$postage = number_format( 17.00, 2);
}
else if ($subtotal > 500) {
$postage = number_format( 12.00, 2);
}
else if ($subtotal > 200) {
$postage = number_format( 7.00, 2);
}
else if ($subtotal < 200) {
$postage = number_format( 0.00, 2);
}
Errors in logic are often the hardest to track down. Follow the logic in the above snippet.
1. If the subtotal is over $3000, do something (OK so far)
2. It falls through to the next else if which is, if the subtotal is over $1500 do something.
3. etc.
Error in logic?
3000 is greater than 1500, 500 and 200. Thus, an order over 3000 will return true on each of those conditional statements. The computer, being stupid, will naturally assign the last value corresponding to a true statement to the variable. Ergo, contrary to the programmer's intent, a $3000 subtotal will have postage of $7.
The computer will sit there, smug in its self assurance that it has done exactly what it was told to do (which of course it has). The programmer will get increasingly frustrated trying to figure out what is making the computer mess up in this way until they:
1. Throw the computer out the window
2. Kick a hole in the wall next to their desk
3. Post to a forum seeking help, which leads to a significant loss of self esteem when the rather obvious answer is returned within moments (simply because a clear head looked at it through fresh eyes ;))
Been there, done that...
WBF
Would I be better off calculating it something like this:
I am not sure how to do the 'is between code'?
if ($subtotal is between 200 & 500) {
$postage = number_format( 20.00, 2);
}
else if ($subtotal is between 500 & 1500) {
$postage = number_format( 17.00, 2);
}
else if ($subtotal is between 1500 & 3000) {
$postage = number_format( 12.00, 2);
}
else if ($subtotal is between 3000 & 5000) {
$postage = number_format( 7.00, 2);
}
else if ($subtotal is between 200 & 500) {
$postage = number_format( 0.00, 2);
}
if ($subtotal > 200 && $subtotal < 500) {
$postage = number_format( 20.00, 2);
}
else if ($subtotal > 500 && $subtotal < 1500) {
$postage = number_format( 17.00, 2);
}
else if ($subtotal > 1500 && $subtotal < 3000) {
$postage = number_format( 12.00, 2);
}
else if ($subtotal > 3000 && $subtotal < 5000) {
$postage = number_format( 7.00, 2);
}
else if ($subtotal > 200 && $subtotal < 500) {
$postage = number_format( 0.00, 2);
}
Would I be better off using the code you suggested or this:
[php]
// calculuate postage rate
if ($_SESSION['state'] == "QLD") {
if ($subtotal >= 0 && $subtotal <= 199) {
$postage = number_format( 0.00, 2);
}
elseif ($subtotal >= 200 && $subtotal <= 500) {
$postage = number_format( 7.00, 2);
}
elseif ($subtotal >= 500 && $subtotal <= 1500) {
$postage = number_format( 10.00, 2);
}
elseif ($subtotal >= 1500 && $subtotal <= 3000) {
$postage = number_format( 13.00, 2);
}
elseif ($subtotal >= 3000 && $subtotal <= 5000) {
$postage = number_format( 15.00, 2);
}
elseif ($subtotal >= 5000 && $subtotal <= 10000) {
$postage = number_format( 0.00, 2);
}
} else {
if ($subtotal >= 0 && $subtotal <= 199) {
$postage = number_format( 0.00, 2);
}
elseif ($subtotal >= 200 && $subtotal <= 500) {
$postage = number_format( 7.00, 2);
}
elseif ($subtotal >= 500 && $subtotal <= 1500) {
$postage = number_format( 12.00, 2);
}
elseif ($subtotal >= 1500 && $subtotal <= 3000) {
$postage = number_format( 17.00, 2);
}
elseif ($subtotal >= 3000 && $subtotal <= 5000) {
$postage = number_format( 20.00, 2);
}
elseif ($subtotal >= 5000 && $subtotal <= 10000) {
$postage = number_format( 0.00, 2);
}
} // end of calculuate postage rate
[/php]
function is_between($var,$num1,$num2)
{
return ($var >= $num1 && $var < $num2)? 1:0;
}
So this is how it should look together:
if ($_SESSION['state'] == "QLD") {if (is_between($subtotal,0,200)) {
$postage = number_format( 0.00, 2);
}
elseif (is_between($subtotal,200,500)) {
$postage = number_format( 7.00, 2);
}
elseif (is_between($subtotal,500,1500)) {
$postage = number_format( 10.00, 2);
}
elseif (is_between($subtotal,1500,3000)) {
$postage = number_format( 13.00, 2);
}
elseif (is_between($subtotal,3000,5000)) {
$postage = number_format( 15.00, 2);
}
elseif (is_between($subtotal,5000,10000)) {
$postage = number_format( 0.00, 2);
}} else {
if (is_between($subtotal,0,200)) {
$postage = number_format( 0.00, 2);
}
elseif (is_between($subtotal,200,500)) {
$postage = number_format( 7.00, 2);
}
elseif (is_between($subtotal,500,1500)) {
$postage = number_format( 12.00, 2);
}
elseif (is_between($subtotal,1500,3000)) {
$postage = number_format( 17.00, 2);
}
elseif (is_between($subtotal,3000,5000)) {
$postage = number_format( 20.00, 2);
}
elseif (is_between($subtotal,5000,10000)) {
$postage = number_format( 0.00, 2);
}} // end of calculuate postage rate
This should work as you want it to ;)