Re: Building a query on multiple variables, how to test for which variable A SOLUTION

From: Date: Wed, 28 Aug 2002 21:10:44 +0000
Subject: Re: Building a query on multiple variables, how to test for which variable A SOLUTION
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-114423@lists.php.net to get a copy of this message
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 column_name='a_different_value'">a_different_value</option> </select> 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> </select> 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.

« previous php.general (#114423) next »