When you say extra variable are you meaning a extra select element?
If so, the idea would be to make the server side code work smarter rather than harder to write.
set an array with the keys that are always the same in a standard form eg: everything apart from your filter selection
create your select boxes with the names/ids set exactly as the columns they search in.
initialise your query almost as in the code above but with a difference, the addition of a placeholder
$selquery = "SELECT fieldlist [filterfields] FROM table"
( No don't use SELECT *  )
PHP Code:
$FirstFilter = false; // set a flag
$FilterFields = ""; //initialise a filter string
// then you loop through the $_POST collection.
foreach ($_POST as $key) {
if (add in a check for $key being in the standard keys array) {
// do nothing & loop around
} else {
$FilterFields = $FilterFields . "," . $key;
if ($_POST[$key] == "Select All") {
// do nothing & loop around
} else {
if (!$FirstFilter) {
$selquery . " WHERE $key = $_POST[$key] "
// note that "$key" will be equal to the "columnname" so the query has now become "SELECT fieldlist [filterfields] FROM table WHERE columnname = value".
$FirstFilter = true
} else {
$selquery . " AND $key = $_POST[$key] "
}
}
}
or something like that in real code
at this point $FilterFields should hold a list of columns that are in the filterable fields and the query should be all the necessary "WHERE"s & "AND"s criteria.
replace the placeholder with the value $FilterFields, close the query off with any ORDER BY etc and you should have a metod that will construct the correct query for any amount of selectors.
What I do with this kind of thing is also add a datatype indicator to the selector name (d_ = date, c_ = char, n_ = numeric) so I can add the appropriate delimiters to the value in the concat loop (remember all POSTed values are strings) and do any "sanitising" if needed.
just regex or "str_replace" these out before concating the key name to the query.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|