Page 1 of 1

if statement decided to stop working

Posted: Thu Feb 09, 2006 8:37 am
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

Posted: Thu Feb 09, 2006 9:02 am
by phpScott
Test to see what $productline is by echo'ing to the screen, maybe the capilization has changed?

Posted: Thu Feb 09, 2006 9:39 am
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")

Posted: Thu Feb 09, 2006 10:35 am
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

Posted: Thu Feb 09, 2006 10:43 am
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)