Page 1 of 1
Simple if then Calculation
Posted: Tue Nov 04, 2003 5:01 pm
by billybongchong
I am calculating a payment based on which country a user lives in. Any help would be muchly appreciated. It is probable a simple little error... I just cannot crack it!
The code is collecting the country variable correclty but when I do the calculations the output of print $delivery = "Countryname.1" ie "Australia.1"..
All I need it to do is output the number ".05" if the user is australian else ".1".
The code is below...Do you have any ideas.
<? $user_country = pv($country);
$au = "Australia";
$delivery = "0";
if ($user_country == "Australia")
$delivery = ".05";
else
$delivery = ".1";
print $delivery;
?>
?>
Posted: Tue Nov 04, 2003 5:34 pm
by d3ad1ysp0rk
Code: Select all
<?PHP
$user_country = pv($country);
$au = "Australia";
$delivery = "0";
if ($user_country == "Australia")
{
$delivery = ".05";
}
else
{
$delivery = ".1";
}
echo $delivery;
?>
I tried this... above
Posted: Tue Nov 04, 2003 6:09 pm
by billybongchong
I tried the suggested code but am getting a parse error on line 10..
// else - this is line 10 ??
Very confusing... I think that it should work.
Posted: Tue Nov 04, 2003 6:10 pm
by microthick
pv() is a private function?
A quick check of google says that pv() might just echo out a variable with special characters escaped.
If that's the case, then I think that $user_country does not hold the info you want.
Try printing the contents of $user_country. I believe it will instead hold nothing.
Posted: Tue Nov 04, 2003 6:26 pm
by billybongchong
$user_country prints the correct information when I include it in the process page....
I tried this as well and the out put = "AustraliaYou...Australia-You Should!.1"
$user_country = pv($country);
$au = "Australia";
$delivery = "0";
if ($user_country == "Australia") {
print ("I like Australia as well");
$delivery = "0.05";
} else {
print ("You don't live in Australia - You Should!");
$delivery = ".1";
}
echo $delivery;
Posted: Wed Nov 05, 2003 11:13 am
by microthick
I still believe this line:
$user_country = pv($country);
Is the problem.
pv($country) is printing "Australia" but it is not assigning any value to $user_country. Why do I think this? Because "Australia" is appearing as the first part of the output, but you are never explicitly echoing $user_country.
So $user_country probably equals "".
To check this theory, change:
$user_country = pv($country);
to
$user_country = "Australia";
and I bet the output will be correct.
If that's the case, then you have to find an alternative to pv($country).
Posted: Sun Nov 09, 2003 4:17 pm
by billybongchong
This is what I ended up with and everything works fine... Thanks guys...
<?
$Host = "localhost";
$User = "xxx";
$Pass = "xxx";
$DataBase = "xxx";
$SQL ="SELECT country FROM table WHERE username = '{$SESSION["user"]["username"]}'" ;
$Connection = mysql_connect($Host,$User,$Pass);
$SQL_Query = mysql_select_db($DataBase, $Connection);
$result = mysql_query($SQL);
$SQL_Array = mysql_fetch_array($result);
$user_country =$SQL_Array ['country'];
//echo $user_country;
// Calulate Percentage Charge for Posting
$au = "Australia";
$subtotal_cost = $SESSION["cart"]->total;
if ($user_country == $au) {
$delivery_percent = .05;
}else{
$delivery_percent = .1;
}
// Mutiplies subtotal with delivery percentage to give total delivery charges
$delivery_charge = $delivery_percent * $subtotal_cost;
?>