homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Three dropdown filters

Msg#: 4461766 posted 7:58 pm on Jun 5, 2012 (gmt 0)

Hi guys, I have a table called classifieds and it contains the following:

id, title, description, state_id, city_id, cat_id

This is dynamically populated from database.

<form name="theForm" method="post" action="advanced_search.php">

<input type="submit" name="Submit" value="Search" />

<select name="categories" >
<option value=""> Select Categoy</option>


<select name="state" >
<option value=""> Select state</option>


<select name="city" >
<option value=""> Select city</option>


So, what I want is. When I select a category and click search, the result for that category will show up. And If I select a category and a state then I want it to be filter to a specified category and state, And same thing if city is selected.

EX: If I select employment category in Minneapolis city, then the search should be filter to all employments in that city.
I gave it a try like this:

if (isset($_POST['Submit'])) {

$select = "SELECT * FROM classifieds";
$where = " WHERE ( authorized = '1')";

$where .= " AND (cat = '" . $categories . "' AND state_id = '" . $state . "' AND city_id = '" . $city . "')";

$where .= " AND (state_id = '" . $state . "' )";
$where .= " AND (city_id = '" . $city . "' )";
$q = $select . $where . ' ORDER BY date DESC';

With this code I must select all dropdowns, and that's not what I want.

I hope someone will walk me through this, and thanks in advance.



WebmasterWorld Senior Member 10+ Year Member

Msg#: 4461766 posted 5:56 am on Jun 6, 2012 (gmt 0)

perhaps if you split it in multiple if statements?
something like this:

if (isset($_POST['city']))
/ /querywith all options
else if (isset($_POST['state']))
// query with first 2 options
// query with 1 option


Msg#: 4461766 posted 3:59 pm on Jun 6, 2012 (gmt 0)

I can't thank you enough. That worked perfectly :)


WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 4461766 posted 5:16 pm on Jun 6, 2012 (gmt 0)

This is going to grow on you (**especially** with something like "classifieds"), and coding a bunch of if/else statements will become unwieldy at best. Try something like this:

// This needs to be globally accessible in a configuration so you can change it.
$inputs = array (
'cat' => 'category',
state_id => 'state',
city_id => 'city',
'description' => 'descr'

$where = null; // so we can concatenate without error
foreach ($inputs as $dbfield=>$formfield) {
if (isset($_POST[$formfield]) and ! empty($_POST[$formfield])) {
// you only need AND if $where has been added to once
if ($where) { $where .= ' and'; }
$where .= " $dbfield='" . mysql_real_escape_string($_POST[$formfield]) . "'"
$query = "select * from classifieds";
if ($where) { $query .= " where $where"; }

You can do similar things to add an order and limit to that.

You might already be wondering about "description" because it's likely a text field and you'd have to decide how you want to partially search that. I usually use four radios ('anywhere' => 'like '%term%', 'starts with' => like 'term%', 'ends with' => like '%term', 'exact' => ='term') and insert it programmatically instead of "=". Topic for another thread (or go with it and figure it out. :-) )

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved