if statement help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

if statement help

Post 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

Code: Select all

Count: Resource id #6

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;
			}  
	       
?>
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post 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

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;form name="test" action="test2.php" method="post" &gt; 
&lt;table width="450"&gt;
		&lt;tr&gt;
			&lt;td width ="200"&gt;First Name: &lt;/td&gt;
			&lt;td&gt;Last Name: &lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;input name="C_First" type="text"&gt;&lt;/td&gt;
			&lt;td&gt;&lt;input name="C_Last" type="text"&gt;&lt;/td&gt;
				&lt;td&gt;&lt;select name="C_Freight"&gt;
                         &lt;option value="Select Freight"&gt;Select Freight&lt;/option&gt;
                         &lt;option value="North Island New Zealand"&gt;North Isl. NZ&lt;/option&gt; 
                         &lt;option value="South Island New Zealand"&gt;South Isl. NZ&lt;/option&gt; 
				    	 &lt;option value="Other"&gt;Other&lt;/option&gt; 
            &lt;/select&gt;&lt;/td&gt;
		&lt;/tr&gt;
&lt;/table&gt;
&lt;input type="submit" name="detailsSend" value="Next" class="next" /&gt;


&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
page 2

Code: Select all

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;/head&gt;

&lt;body&gt;

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

&lt;table width="450"&gt;
		&lt;tr&gt;
			&lt;td width ="200"&gt;First Name: &lt;/td&gt;
			&lt;td&gt;Last Name: &lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;?php echo $C_First=$_POST&#1111;'C_Last']; ?&gt;&lt;/td&gt;
			&lt;td&gt;&lt;?php echo $C_Last=$_POST&#1111;'C_Last']; ?&gt;&lt;/td&gt;
			&lt;td&gt;shipping-&lt;?php echo $shipping?&gt;&lt;/td&gt;
		&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
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
Last edited by C_Calav on Sat Dec 04, 2004 9:20 pm, edited 1 time in total.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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:)
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thatnx i will go back and check what is getting posted. thanx
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

yep
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post 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>?
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

if you use a switch case that will only allow 1 case so that may solve your problem
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post 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
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post 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 ?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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'];
Post Reply