Forum Moderators: coopster

Message Too Old, No Replies

help with if else statement

         

adammc

11:14 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



Hi folks,

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.

willybfriendly

11:21 pm on Jun 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Count your curly brackets. Looks like you are missing a couple closing brackets to me.

Are you getting error messages?

WBF

adammc

11:43 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



thanks, I missed that ;)

I havent tested it as yet, as I know that my if else statements arent the correct form.

jatar_k

11:50 pm on Jun 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it won't work

your first is > 100

both other cases will get caught by that as well, since >200 and >300 would also cause >100 to be true

eelixduppy

12:36 am on Jun 6, 2006 (gmt 0)



>>>>both other cases will get caught by that as well, since >200 and >300 would also cause >100 to be true

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!

willybfriendly

4:54 am on Jun 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Always a good idea to have a default. Something like

else die "QLD is not valid";

WBF

(PS, you may not want to kill the script, but some default is good to have)

adammc

5:07 am on Jun 6, 2006 (gmt 0)

10+ Year Member



Thanks for your help guys you have been a GREAT help :)

adammc

3:50 am on Jun 8, 2006 (gmt 0)

10+ Year Member



Hi Guys,

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]

vincevincevince

4:12 am on Jun 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This should calculate it:
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)

adammc

4:24 am on Jun 8, 2006 (gmt 0)

10+ Year Member



Hi vince, thanks for the reply.

How would i use that function, sorry I am only reasonably new to PHP.

Thanks for the tips on my coding style ;)

vincevincevince

5:08 am on Jun 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Lucky you asked - I had the function wrong.

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;

adammc

5:54 am on Jun 8, 2006 (gmt 0)

10+ Year Member



Thank you Soooooo much vince, you are a PHP GOD :)

adammc

6:04 am on Jun 8, 2006 (gmt 0)

10+ Year Member



One last question...

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

willybfriendly

7:48 am on Jun 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

adammc

12:17 am on Jun 9, 2006 (gmt 0)

10+ Year Member



Hi Willy,
Yeah, have a few holes in my office wall ;)

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);
}

eelixduppy

2:22 am on Jun 9, 2006 (gmt 0)




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);
}

This should work ;)

adammc

2:33 am on Jun 9, 2006 (gmt 0)

10+ Year Member



thanks for the reply eelixduppy :)

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]

eelixduppy

11:13 am on Jun 9, 2006 (gmt 0)



Well...You have overlapping domains so I'm unsure what you want to do with that. You could also create a function to do this for you:

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 ;)