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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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