0

i'm trying to create a filter using jQuery / Ajax and PHP/MySQL.

I have 4 dropdown select in my HTML, i want to achieve something like this:

When Ajax success post data, my PHP file check to see if value is empty or not, so each dropdown will add 'AND' statement to the SELECT query, something like:

SELECT * from properties WHERE property_id = '$property_id' AND dropdown_value = '$dropdown_value' AND dropdown_value2 = '$dropdown_value2'

So i could set all the variables and just check if the posted value is empty or not, so if it is empty, the query statement is not added at all.

Is this possible?

Thanks.

EDIT

Current code:

$statements = array();
if (isset($_POST['segmento'])) {
   $segmento_query = $_POST['segmento'];
   $statements[] = " AND property_details.segmento = '$segmento_query' "; //condition for each property
}
if (isset($_POST['cidade'])) {
   $cidade_query = $_POST['cidade'];
   $statements[] = " AND property_details.cidade = '$cidade_query' ";
}

$filtraSegmento = "SELECT * FROM properties, property_complements, property_details WHERE property_complements.imovel_id = properties.property_id AND property_details.imovel_id = properties.property_id $statements";

1 Answer 1

1

Have you tried:

$statements = array();
if (isset($POST['param1']) {
   $param1 = $POST['param1'];
   $statements[] = " property1 = '$param1' "; //condition for each property
}
if (isset($POST['param2']) {
   $param2 = $POST['param2'];
   $statements[] = " property2 = '$param2' ";
}

//.....more.....
$sql = "SELECT * FROM tbl WHERE ". implode('AND', $statements);
//do something here

Edited: use $statements as a string

$statements = '';
if (isset($_POST['segmento'])) {
   $segmento_query = $_POST['segmento'];
   $statements .= " AND property_details.segmento = '$segmento_query' "; //condition for each property
}
if (isset($_POST['cidade'])) {
   $cidade_query = $_POST['cidade'];
   $statements .= " AND property_details.cidade = '$cidade_query' ";
}

$filtraSegmento = "SELECT * FROM properties, property_complements, property_details WHERE property_complements.imovel_id = properties.property_id AND property_details.imovel_id = properties.property_id $statements";
5
  • I have just tried your code, but it's not working, does not return the data. i've updated my question with the current code with your code. Commented Jul 28, 2015 at 2:33
  • it doesn't work because you use $statements as a string, I have edited my answer with 2 ways: 1. use array and implode, 2. use string concatenation
    – Thi Tran
    Commented Jul 28, 2015 at 2:39
  • Ok, i just tried again as string, looks like it only understand when is empty, so the statement is not added, but when there's value, it does not work. Commented Jul 28, 2015 at 2:42
  • please show me $filtraSegmento's value before execute sql
    – Thi Tran
    Commented Jul 28, 2015 at 2:45
  • Forgive me dude, it's working, i forgot to remove the [] characters as i'm using strings as you second sugestion, my bad, it's working like a charm, thanks a lot for your help :) Commented Jul 28, 2015 at 2:46

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.