if statement decided to stop working

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
thurstan
Forum Commoner
Posts: 28
Joined: Mon Feb 28, 2005 7:40 am

if statement decided to stop working

Post by thurstan »

I've had this peice of code working nicely for a while now, but all of a sudden, it's decided not to work. I can't figure out what has changed, as i haven't changed the code. here's the code:

the site only diaplys the results from the first if statment, every time, and spits this out for every option, it seems the elseif's have stopped working.

if somebody can have a look out for something that shouldn't be there i'd be grateful:

Code: Select all

//main if statement
$productline=$row[productline];
if ($productline=="shoes" or "boots")
{
//shoes block
print ("<p class=\"shop_detail\">
<strong>Name:</strong> $row[product_make]<br />
<strong>Style:</strong> $row[style_name]<br />
</p>
<h2>Customise your shoes</h2>
<p>Pick size and colour</p>
<p><strong>Size:</strong></p>
<form action=\"http://www.fastcart.co.uk/cart/index.php\" method=\"post\">
<input type=\"hidden\" name=\"id\" value=\"9272\">
<select name=\"custom[size]\">
<option>$row[product_make] $row[style_name] - size $row[size1]</option>
</select>
<p><strong>Colour:</strong></p>
<select name=\"custom[colour]\">
<option>($row[colour1])</option>
</select><br />
<input type=\"hidden\" name=\"price\" value=\"$row[price]\"><br />
<input type=\"image\" src=\"/gfx/shop/buy_now_button.gif\" class=\"buy_button\" value=\"Add to Cart\">
</form>");

//elseif for scarves
}

elseif($productline=="scarves")
{
//scarves block
print ("<p class=\"shop_detail\">
<strong>Name:</strong> $row[product_make]<br />
<strong>Style:</strong> $row[style_name]<br />
<strong>Descripton:</strong> $row[desc_short]<br />
print ("
</p>
<a href=\"http://www.fastcart.co.uk/cart/index.php?id=9272&item=$row[product_make]+$row[style_name]&price=$row[price]\"><img class=\"buy_now\" src=\"../../gfx/shop/buy_now_button.gif\" /></a>");

//elseif for handbags
}
elseif($productline=="bags")
{
//scarves block
print ("<p class=\"shop_detail\">
<strong>Name:</strong> $row[product_make]<br />
<strong>Style:</strong> $row[style_name]<br />
<strong>Descripton:</strong> $row[desc_short]<br />
<strong>Colour:</strong> $row[colour1]<br />
<strong>Price:</strong> &pound;$row[price]<br />
print ("
</p>
<a href=\"http://www.fastcart.co.uk/cart/index.php?id=9272&item=$row[product_make]+$row[style_name]&price=$row[price]\"><img class=\"buy_now\" src=\"../../gfx/shop/buy_now_button.gif\" /></a>");

}
elseif($productline=="hats" or "gloves")
{
//non shoes block
print ("<p class=\"shop_detail\">
<strong>Name:</strong> $row[product_make]<br />
<strong>Colour:</strong> $row[colour1]<br />
<strong>Price:</strong> &pound;$row[price]<br />");
print ("
</p>
<a href=\"http://www.fastcart.co.uk/cart/index.php?id=9272&item=$row[product_make]+$row[style_name]&price=$row[price]\"><img class=\"buy_now\" src=\"../../gfx/shop/buy_now_button.gif\" /></a>");
}
else
{
print "error!";
}
thankyou
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

Test to see what $productline is by echo'ing to the screen, maybe the capilization has changed?
sheila
Forum Commoner
Posts: 98
Joined: Mon Sep 05, 2005 9:52 pm
Location: Texas

Post by sheila »

The if test is not doing what you think
Here's a test script

Code: Select all

$productline = 'xxx';
if ($productline=="shoes" or "boots")
{
    echo "shoes block ";

} else {
    echo "other block";
}
This prints 'shoes block'. It doesn't matter what $productline is set to.

You need to rewrite the test as

Code: Select all

if ( ($productline=="shoes") or ($productline =="boots") )
Change this line too

Code: Select all

elseif($productline=="hats" or "gloves")
thurstan
Forum Commoner
Posts: 28
Joined: Mon Feb 28, 2005 7:40 am

Post by thurstan »

sheila - thanks so much - this works perfectly. the double brackets seem to be the factor here. i shall look up what this means. thanks again :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the "brackets" have nothing to do with it. The logic you were trying wasn't correct for how the language works.

Code: Select all

$productline=="hats" or "gloves"
doesn't equate to it checking if $productline is equal to 'hats' or 'gloves'. Instead is means, check $productline if it's equal to 'hats', otherwise use the boolean value of 'gloves' (which is true, always)
Post Reply