I have done this two different ways.
The first involves a function that creates a dropdown from the database.
What is special about this function is that I assign it's own condition as
part of the value for that option.  Take this sql statement: select distinct
column_name from the_table
and it would generate this for a select statement:
<select name="column_name">
<option value=" ">All</option>
<option value=" and column_name='some_value'">some_value</option>
<option value=" and column_name='another_value'">another_value</option>
<option value=" and

Then, when the form is processed, the sql statement would be as follows:
$sql="select whatever_columns from the_table where 1=1$column_name";

The second method is somewhat similar in that the sql statement would remain
the same, but you would have to check each select value to see if you needed
to add a "and column_name ='$column_value'" to it.  i.e.
select statement would be like:
<select name="column_name">
<option value=" ">All</option>
<option value="some_value">some_value</option>
<option value="another_value">another_value</option>
<option value="a_different_value">a_different_value</option>

processing would be like:
if (trim($column_name))
    $column_name=" and column_name='$column_name'";
$sql="select whatever_columns from the_table where 1=1$column_name";

The important part on this is that you don't have to be concerned with
whether or not to use and or where on the statement, always use and, and in
your sql statement put a where clause that is always true.  i.e. where 1=1.

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to