Jump to content

How to make a search with many options


feri_soft

Recommended Posts

How to make a search with many many many many options and if some of the are not set to asume that any is posssible.For exemple i have a real-estates website and in search i have many options if flour for example is not set we the script will consider that any flour is possible???

Help me fast please!
Link to comment
Share on other sites

Let's assume you have two fields; 'roomsr for number of rooms and 'pool' for wether the property has a pool or not. On the search page you could create a select list for rooms with the first entry label being "Any" and a value of "*". Each of the other entries will have the same label and values (1, 2, 3...). Then you could have a select list for Pool with the following labels/values:
Any/*
Yes/1
No/0

Then on the page that performs the actual search you would just create your query as follows:

[code]<?php
$query = "SELECT * FROM 'properties' WHERE 'rooms' = $POST_['rooms'] AND 'pool' = $POST_['pool']";
?>[/code]

This is a very simplistic example, but you should get the idea. Some criteria may require additional processing before creating your query. For example what if the number of rooms was to be a miniumum:

Then you could do something like this:[code]<?php
if ($POST_['rooms'] <> "*") {
    $roomQuery = " 'rooms' > " . $POST_['rooms'] . " AND";
} else {
    $roomQuery = ""
}

$query = "SELECT * FROM 'properties' WHERE" . $roomQuery . " 'pool' = $POST_['pool']";?>[/code]
Link to comment
Share on other sites

One example, this depends that your postform input fields are named exactly the same as the columns you want to search in your table[code]

<?php

$query = "select * from table";

$count = 0;
foreach($_POST as $key => $value)
{
if($count == 0): $qq = " WHERE"; else: $qq = " AND"; endif;
if(!empty($value)): $query .= "$qq $key = '$value'"; endif;
$count++;
}
$query .= " order by id desc";

$sql = mysql_query($query) or die(mysql_error());

?>

[/code]

Work with it and adjust it to your needs...
Link to comment
Share on other sites

Another thing i havent achieved many times is for example i have 10 checkboxes how if i chose 3 of them to search for 3 different property types.In other words how dynamicaly to create all the OR statements.Something with isset? Because i dont want to send the input as array.I want every checkbox with different name.
Link to comment
Share on other sites

Here's an example of the method that I use

http://www.phpfreaks.com/forums/index.php/topic,89842.msg360739.html#msg360739

Checkboxes

[code]
<?php
if (isset($_POST['proptype'])) {
    $ptypes = join ("','", $_POST['proptype']);
    $where[] = "(property_type IN ('$ptypes'))";
}

?>
<form method='post'>
Search for property type<br>
<input type="checkbox" name="proptype[]" value="Villa"> Villa <br>
<input type="checkbox" name="proptype[]" value="Bungalow"> Bungalow <br>
<input type="checkbox" name="proptype[]" value="Shack"> Shack <br>
<input type="submit" name="action" value="Search">
</form>
[/code]

This gives

...WHERE property_type IN ('Villa','Shack')

which is a simpler form than

... WHERE ((property_type = 'Villa') OR (property_type = 'Shack'))
Link to comment
Share on other sites

hmm nice but how to include include the variable to the query :

select * from properties where $where[] and asdasdasd ='123' or how???


i have another very hard to make thing too:

See this site: imoti.bg (pick the en version). I use your class baaselect for making the town and district chained.But how to make the search in many regions like in this site with the too buttons >> and <<
I know there is a javascript for the buttons functionality but i am interested in the php aspect?
Link to comment
Share on other sites

Ok i made it like this:

[code]if (count($where) > 0){
$whereclause = " WHERE " . join (' AND ', $where);
}else{
echo "Chose at least 1!";
}
$t = mysql_query("SELECT * FROM listings $whereclause");[/code]

But now i cant made all the joints because i have in the databse for example type,district,city,bildtype
and all these are numbers.I have databse tables type districts cities buildtype but when i try something like
select listings.*,type.* ..... from listings,type...... and in the whereclause i add
$whereclause = " WHERE listings.city=cities.city(which is the id field) and listings.district=districts.district and ...." . join (' AND ', $where);
i got unknown field in the where clause .... for each field,but the field names are exact.Can you help me with the join.I saw something for the inner joins but i dont know how to make multiple inner joins with 1 query and which exacty to select,because if i have manually to add in the select every field not using the * i must add about 50 fields.Help me please! :)
Link to comment
Share on other sites

Now i ahve something like:

[code]$q = mysql_query("SELECT listings.*, cities.name, districts.name FROM listings INNER JOIN cities ON listings.city = cities.city INNER JOIN districts ON listings.district = districts.district $whereclause");[/code]

and when i try to view {$row['city']} i get the number... for example 1
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.