Forum Moderators: coopster

Message Too Old, No Replies

Conditional Expression Problem

         

rjbearcan

2:45 pm on Aug 7, 2005 (gmt 0)

10+ Year Member



I am trying to list products that are greater than $100 and less than $250. Yet the expression isn't working.

if ( $price >= "$100" ) && ( $price <= "$250" ) echo "$item<br> "; else echo " ";

Obviously if I set my query to
$query = "SELECT * FROM products WHERE price > '$100' AND price < '$250' ORDER BY id ASC";
it works. Can anyone point me in the right direction?

Thanks.

jatar_k

3:12 pm on Aug 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it may just be your syntax

if ( $price >= "$100" ) && ( $price <= "$250" )

this will actually try to resolve the $ sign and think those are vars, try changing the double quotes around those to single quotes and see what happens

if ( $price >= '$100' ) && ( $price <= '$250' )

the other thing I wonder, is the $ actually in the column? It could produce some strange results as that makes it a string comparison as opposed to a numeric one.

rjbearcan

4:08 pm on Aug 7, 2005 (gmt 0)

10+ Year Member



The $ was in the column but I took it out and I don't think it really made a difference.

If I use just a simple if statement such as
if ($price >= '100') echo "$item"<br> "; else echo " ";

it works just fine. But as soon as I add in the
&& ($price <= '250')

it doesn't. This happened whether the $ was in the column or not.

jatar_k

4:12 pm on Aug 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



aha

sorry my fault hadn't had my first coffee yet, now I've had 2 we're good to go

it's your parentheses, thr grouping is messed up, though you still need to leave those as single quotes as well

if ( $price >= '$100' ) && ( $price <= '$250' )

you need to contain all of that in your if so you need an extra set

if (($price >= '$100') && ($price <= '$250'))

rjbearcan

8:03 pm on Aug 7, 2005 (gmt 0)

10+ Year Member



Excellent, it works great. Thanks a lot!