Solarpitch Posted November 6, 2007 Share Posted November 6, 2007 Hey guys, I have the below code that takes 3 variables as part of a user search but on the &mode below, this keeps defaulting to the last option which is 4. Anyone spot anything I cant? $type = $_POST['category']; $location = $_POST['area']; $price = $_POST['price']; --> Say this is set as "3" ... the mode will always set to 4 if($price = 1); { $mode = "price <= 100000"; } if($price = 2); { $mode = "price BETWEEN 100000 AND 200000"; } if($price = 3); { $mode = "price BETWEEN 200000 AND 300000"; } if($price = 4); { $mode = "price BETWEEN 300000 AND 400000"; } // Search box code function getprice(){ dbconnect(); $select = "<select name=\"price\" id='listboxes'><optgroup label=\"Select ...\""; $sql = "select * from getprice"; $result = mysql_query($sql); while(($row = mysql_fetch_row($result)) != false){ $select .= "<option value=\"".$row[0]."\">".$row[1]."</option>"; } $select .= "</select>"; return $select; } // Query in question $sql = "select * from property_info where type = ".$type." and location = ".$location." and ".$mode.""; Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted November 6, 2007 Share Posted November 6, 2007 Yup, you have to use == and not = when comparing values/variables: if($price = 1) {} if($price == 1) {} EDIT: If the problem is $_POST['price'] always holding the value 4 (even though the user types / selects 3) you will have to show us the form... Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 6, 2007 Author Share Posted November 6, 2007 I've tried the variables like that already and it still doesnt seem to work correctly. Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 6, 2007 Share Posted November 6, 2007 As Wuhtzu said, that's going to be a form submission problem, so post the form. PhREEEk Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted November 6, 2007 Share Posted November 6, 2007 Yup, post us the output of your getprice() function which seems to output the select box... EDIT: Just to be sure, you are now using something like this: <?php $type = $_POST['category']; $location = $_POST['area']; $price = $_POST['price']; --> Say this is set as "3" ... the mode will always set to 4 if($price == 1); { $mode = "price <= 100000"; } if($price == 2); { $mode = "price BETWEEN 100000 AND 200000"; } if($price == 3); { $mode = "price BETWEEN 200000 AND 300000"; } if($price == 4); { $mode = "price BETWEEN 300000 AND 400000"; } ?> Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 6, 2007 Author Share Posted November 6, 2007 Hey, Here's the HTML output from that function.. <select name='price' id='listboxes'> <option value='1'>Up to €100,000</option> <option value='2'>€100,000 - €200,000</option> <option value='3'>€200,000 - €300,000</option> <option value='4'>€300,000 - €400,000</option> <option value='5'>€400,000 +</option> </select> Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted November 6, 2007 Share Posted November 6, 2007 Another thing you need to clarify: $price = $_POST['price']; --> Say this is set as "3" ... the mode will always set to 4 What exactly do you mean? In the code you have shown us the mode ($mode) is never set to 1, 2, 3 or 4. It is set to for example $mode = "price BETWEEN 100000 AND 200000";. So do you mean if the user selects < option value='3'>€200,000 - €300,000< /option> the $_POST['price'] will always be set to '4' and therefore $mode always $mode = "price BETWEEN 300000 AND 400000";?? Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 6, 2007 Author Share Posted November 6, 2007 Yes, thats exactly what I mean. Sorry... I ment to say price that time Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted November 6, 2007 Share Posted November 6, 2007 Nothing seems out of the ordinary. $_POST['price'] should contain the value '3' if the user chooses < option value='3'>€200,000 - €300,000< /option> and if you usee == to compair the values in your if() statement the $mode variable should be set too... What happens if you set $price = 3; in our code (hard code it)? Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 6, 2007 Author Share Posted November 6, 2007 Umm... even when I hard code it it still doesnt seem to work select * from property_info where type = 2 and location = 2 AND price >= 400000 Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 6, 2007 Author Share Posted November 6, 2007 I took out option 4.. if($price == 4); { $mode = "AND price BETWEEN 300000 AND 400000"; } But then it defaults to 3 Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted November 6, 2007 Share Posted November 6, 2007 I don't know why I haven't noticed, but your if() statements is still wrong syntax.... <?php //Wrong if($price = 4); { } //Correct if($price == 4) { } ?> So the reason why your code do what it does is because even when for example if($price == 2) is true it does nothing because you have ended the statement with ; before you got to the part where you specify what to do. And apparently { $mode = "something"; } works even with the brackets around it. So you just keep on reassigning new values to $mode until it finally is assigned $mode = "price BETWEEN 300000 AND 400000"; By the way use a switch instead of all those if statements: <?php switch($price) { case 1: $mode = "price <= 100000"; break; case 2: $mode = "price BETWEEN 100000 AND 200000"; break; case 3: $mode = "price BETWEEN 200000 AND 300000"; break; case 4: $mode = "price BETWEEN 300000 AND 400000"; break; } ?> Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 post your recent code? Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 6, 2007 Author Share Posted November 6, 2007 Oh for f@#k sake! How did I not even notice that myself? Probably about 20 people looking at this going... "I wonder whats wrong with it" Cheers man!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.