Forum Moderators: coopster

Message Too Old, No Replies

simple IF statement in PHP ...

trying to establish free 'shipping' in a shopping cart

         

davidhorn01

11:15 am on Mar 23, 2004 (gmt 0)

10+ Year Member



Hi -

I'm implementing a shopping cart in PHP which is going okay, but I've run into a problem with what should be a simple IF statement. The statement is below.

It should be that if the field 'postcode1' has been completed with the text BT93, then shipping is free ... otherwise, return $20. But it doesn't work. It's evaluating everything as false and returning a value of $20 for all postcodes.

Any help would be greatly appreciated,

Thanks.

{
global $HTTP_SESSION_VARS;
$deliverypostcode = $HTTP_SESSION_VARS['postcode1'];

if ($deliverypostcode == 'BT93')
{
return 0.00;
}
else
{
return 20.00;
}
}

justageek

11:48 am on Mar 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there a value for $deliverypostcode if you echo it out? Does the user choose the code from a drop down or something or just type it in? If the value they type in is 'bt93' instead of 'BT93' it could fail because of case comparison.

JAG

Birdman

11:53 am on Mar 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to "return" a value, you have to do it with a function and then call the function where you want the value returned.

function getShipping($post){
if($post == "BT93") {
return 0.00;
} else {
return 20.00;
}
}

Now call the function:

Shipping: $<?php getShipping($_SESSION["'postcode1"])?>

Note that I used the $_SESSION global. $HTTP_SESSION_VARS is deprecated and should only be used on old versions of PHP>

davidhorn01

3:32 pm on Mar 23, 2004 (gmt 0)

10+ Year Member



Thanks for the replies ... no, it doesn't appear if I echo it out, which is really odd.

I am using it within a function:

function calculate_shipping_cost()
$delpostcode = $HTTP_SESSION_VARS['postcode1'];
if ($delpostcode == 'BT93')
{
return 0.00;
}
else
{
return 20.00;}

and then calling it from where the cart totals up all the items etc.

It's really stumping me because it doesn't look that difficult!

Thanks again,

David

mykel79

4:36 pm on Mar 23, 2004 (gmt 0)

10+ Year Member



no, it doesn't appear if I echo it out, which is really odd

Well then, there's your problem. It's not the IF statement that's buggy. The variable is not being set correctly. Are other session variables set correctly? Do they appear when you echo them out? How is this variable set?

Warboss Alex

4:37 pm on Mar 23, 2004 (gmt 0)

10+ Year Member



You are -making- a call to session_start, right? I'm assuming that the function is on a page with sessions enabled, 'cause when you said it didn't echo anything out, it rather reminded me of a variable which hasn't been set ..

The following code worked fine for me:

<?php

session_start();

function calculate_shipping_cost() {

$delpostcode = $_SESSION['postcode1'];
if ($delpostcode == "BT93")
{
return 0.00;
}
else
{
return 20.00;}
}

$_SESSION['postcode1'] = "BT93";
printf("%01.2f", calculate_shipping_cost());
print "<br />";
$_SESSION['postcode1'] = "ABCDEF";
printf("%01.2f", calculate_shipping_cost());

?>

You should get an output of
0.00 [no charge for a postcode of BT93]
20.00 [$20 charge for any other postcode]

Your original code worked too, using the old HTTP_SESSION_VARS array, but that's not good practice anymore. I had to do a session_start at the top of the file..

The problem is, as far as I can see, that $_SESSION['postcode1'] (or $HTTP_SESSION_VARS['postcode1']) just wasn't set.. especially if it didn't echo anything.

Nova Reticulis

4:36 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



Hi!

$HTTP_SESSION_VARS[] is deprecated as of PHP 4.0.7. Better use $_SESSION[]. And anyway, is your session started? Was it started when you set the value during previous script invocation?