Jump to content

[SOLVED] Search function question?


Solarpitch

Recommended Posts

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."";


Link to comment
https://forums.phpfreaks.com/topic/76280-solved-search-function-question/
Share on other sites

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...

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";
}
?>

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>

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";??

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)?

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;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.