Sure . . . ok my "$expected_input" array was arbitrary and only an example. Basically,
// Map the input to the fields. You aren't using field
// names in your forms . . . ARE YOU? Don't.
$expected_input = Array (
'price' => 'pricefield',
'bedrooms' => 'bedroomfield',
'type' => 'property_type'
);
I am making the
assumption that you have a field named
price in your form, and this field will query the database field named
pricefield in your properties table. So you build this array associating form field => database field. This does a couple things, it allows you to eliminate any other input, increasing security *a little,* and it doesn't reveal your database table field names to the public, also increasing security a little. Best of all, it allows us to automate and easily update our select statement. Need to add a new form field and database field? We just add a new member to the array. Again, just an example, you'll need to build this yourself.
$expected_input = Array (
'price' => 'pricefield',
'bedrooms' => 'bedroomfield',
'type' => 'property_type',
'sq_ft' => 'square_footage'
);
So we start with this,
$select = "select * from table";
(note the correction, missed * in the original example) and if nothing is input from the form, it will select all records, which is what you have now.
However, if the user selects something, we will add a where clause to that. We start by setting our $where variable to null.
$where = null;
The reason: we are going to
concatenate (add to) this variable as needed. Concatenate is to add to a string.
$blah = 'This is';
echo $blah; // prints "This is"
$blah .= ' a sample';
echo $blah // prints "This is a sample"
$blah .= ' of concatenation';
echo $blah; // prints "This is a sample of concatenation"
If you concatenate to an undefined variable, you get an undefined variable warning, harmless but fugly. The other reason we want to preset $where to null is that we can simplify our code with
if ($where) {.....
Which returns false if it's set to null. Besides all that, it's just good practice to predefine your variables (however trivial.)
So let's build our where clause. We begin by looping through our "expected input" which follows rule 1 of 'net security (in a basic form): accept only what you expect and throw everything else away.
foreach ($expected_input as $val => $fieldname) {....
So the first one will be "price" in $val (form name "price") and "pricename" in $fieldname(database table field name.)
We see if 1) input from the form has been set (something selected/entered) and 2) if it is set, check if it's not empty. We need to check empty because text fields will still exist in $_POST/$_GET - they'll just be empty.
if (isset($_POST[$val]) and ! empty($_POST[$val])) { ....
If something is there, we can add it to our $where variable. Here's a couple examples.
... where this='that'
Note there's no "and" for the first one (duh. :-) ) But if we match on two fields, we need an "and."
... where this='that' and these='those'
So if a value has been set from the form, we add an "and" for everything but the first selection. Remember we set $where to null?
if ($where) { $where .= ' and'; }
$where .= " $fieldname='$val'";
If nothing is input, $where stays null. If something is input, it will now be set to a string. The first time through, it sets where to something, **if** there is input from the form. First time through, if price is entered,
pricefield='123000'
second time through, if property type selected,
pricefield='123000' and property_type='Residential'
.. and so on. So we finish with
if ($where) { $select .= " where $where"; }
So this can be any number of final selects:
select * from table // no input
select * from table where pricefield='123000' // price only
select * from table where pricefield='123000' and property_type='Residential'
select * from table where pricefield='123000' and property_type='Residential' and bedrooms='7' // got lots of kids huh?
The original code should do all that, just add the * I missed. It goes where your original statement is:
$sql = "SELECT * FROM property";
The only difference being, I used the variable $select out of habit, use whatever you like.
There are other things you'll need to learn, like cleansing your input, and how to add order and limit clauses with pagination . . . but get past this first.