Page 1 of 1
if statement help
Posted: Sat Dec 04, 2004 8:25 pm
by C_Calav
hi guys, i m having some trouble with this "if" statement.
if they click "North Island" they get on screen for shipping = 30 and totalcost = 340
if they click "South Island" they get on screen for shipping = 20, and totalcost = 320
if they click "other" they get on screen for shipping = 30 and totalcost = 330
now some of these figures dont add up and sopme are the wrong options can anyone help me with what im doing wrong or why htis is?
also the sql runs fine in mySQL but when i echo on website it returns
thanx guys
Code: Select all
<?php
$count = mysql_query("SELECT count(*) FROM cart WHERE cookieId = '$cartId'");
$C_Freight=$_POST['C_Freight'];
if ($C_Freight == 'North Island New Zealand')
{
$shipping = 10;
$totalCost = $totalCost + $shipping;
}
if ($C_Freight == 'South Island New Zealand')
{
$shipping = 20;
$totalCost = $totalCost + $shipping;
}
else
{
$shipping = 30;
$totalCost = $totalCost + $shipping;
}
?>
Posted: Sat Dec 04, 2004 8:58 pm
by dull1554
according to what you said should it not be?
Code: Select all
<?php
$count = mysql_query("SELECT count(*) FROM cart WHERE cookieId = '$cartId'");
$C_Freight=$_POST['C_Freight'];
if ($C_Freight == 'North Island New Zealand')
{
$shipping = 30;
$totalCost = $totalCost + $shipping;
}
if ($C_Freight == 'South Island New Zealand')
{
$shipping = 20;
$totalCost = $totalCost + $shipping;
}
else
{
$shipping = 30;
$totalCost = $totalCost + $shipping;
}
?>
was that was was wrong??
if not tell a bit more specifically what is happening because you syntax is perfect
Posted: Sat Dec 04, 2004 9:10 pm
by C_Calav
thanx for the reply dull1554,
this is how i want it, but the output comes out different. like the results i first posted.
i took the code out of my page to test it by its self and it still comes out funny. here is the code on its own in one page.
page 1
Code: Select all
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="test" action="test2.php" method="post" >
<table width="450">
<tr>
<td width ="200">First Name: </td>
<td>Last Name: </td>
</tr>
<tr>
<td><input name="C_First" type="text"></td>
<td><input name="C_Last" type="text"></td>
<td><select name="C_Freight">
<option value="Select Freight">Select Freight</option>
<option value="North Island New Zealand">North Isl. NZ</option>
<option value="South Island New Zealand">South Isl. NZ</option>
<option value="Other">Other</option>
</select></td>
</tr>
</table>
<input type="submit" name="detailsSend" value="Next" class="next" />
</form>
</body>
</html>
page 2
Code: Select all
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
Code: Select all
<?php
$C_Freight=$_POST['C_Freight'];
if ($C_Freight == 'North Island New Zealand')
{
$shipping = 10;
}
if ($C_Freight == 'South Island New Zealand')
{
$shipping = 20;
}
else
{
$shipping = 30;
}
?>
Code: Select all
<table width="450">
<tr>
<td width ="200">First Name: </td>
<td>Last Name: </td>
</tr>
<tr>
<td><?php echo $C_First=$_POSTї'C_Last']; ?></td>
<td><?php echo $C_Last=$_POSTї'C_Last']; ?></td>
<td>shipping-<?php echo $shipping?></td>
</tr>
</table>
</body>
</html>
and the results are
north island = 30
south oisland = 20
other =30
should be
north island = 10
south oisland = 20
other = 30
does this clear up my problem? basically the code above prints out not what want
Posted: Sat Dec 04, 2004 9:20 pm
by dull1554
ok so that narrows it down, for some reason North Island New Zealand is never getting returned to the php script so it is either South Island New Zealand or it falls into else thats why u get 30,20,30
that should help a little narrow your search, i looked but i saw no problems so all i can say to do is to check and re check maybe change North Island New Zealand to something like "ninz" or somthing like that...
i guess thats all the help i have for you:)
Posted: Sat Dec 04, 2004 9:24 pm
by C_Calav
thatnx i will go back and check what is getting posted. thanx
Posted: Sat Dec 04, 2004 9:26 pm
by dull1554
yep
Posted: Sat Dec 04, 2004 9:27 pm
by C_Calav
nope that didnt work, changing what gets posted to something smaller and one word..
can anyone else see anything? should i change it to a switch statment or somehting>?
Posted: Sat Dec 04, 2004 9:28 pm
by dull1554
if you use a switch case that will only allow 1 case so that may solve your problem
Posted: Sat Dec 04, 2004 9:30 pm
by C_Calav
ok ill go try and code a switch case.. i havnt done one b4 thats why i used a if.. and thought it is more apropretate
Posted: Sat Dec 04, 2004 9:44 pm
by C_Calav
ok,
Code: Select all
<?php
$C_Freight=$_POST['C_Freight'];
switch ( $C_Freight )
{
case "ninz":
$shipping = 10;
break;
case "sinz":
$shipping = 20;
break;
default:
$shipping = 30;
break;
}
?>
this works how is should now.
the other question i had was:
when i echo this on the page
Code: Select all
$count = mysql_query("SELECT count(*) FROM cart WHERE cookieId = '$cartId'");
i get
Count: Resource id #6
when i run the sql in MySQL i get: 3
(whicxh is correct, counts hoew many items in the cart)
anyone know why Resource id #6 comes up ?
Posted: Sun Dec 05, 2004 9:16 am
by timvw
Code: Select all
$count = mysql_query("SELECT count(*) FROM cart WHERE cookieId = '$cartId'");
make that:
Code: Select all
$result = mysql_query("SELECT COUNT(*) as count FROM cart ... ");
$row = mysql_fetch_assoc($result);
echo $row['count'];