Forum Moderators: coopster
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;
}
}
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>
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
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.