Re: Building a query on multiple variables, how to test for which variable
| From: | @ Edwin | Date: | Wed, 28 Aug 2002 19:31:53 +0000 |
| Subject: | Re: Building a query on multiple variables, how to test for which variable | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-114415@lists.php.net to get a copy of this message | ||
There must be a better way how to do it but this is what I would do: (Not tested--just the "idea"...)
Instead of $var1, $var2... I'd use an array for the drop downs thus,
$var[]
$var[]
$var[]
$var[]
Then, check if any is set, then... anyway, this is how it goes:
// from here
$query = "SELECT foo ";
$query .= "FROM bar";
if (isset($_POST['var']){ // if POSTed
$its_set = 1; // my super "switch"
foreach ($var as $key => $value){
if ($its_set == 1){
$query .= " WHERE thus = '" . $value . "'";
$its_set = 2; // just to make sure "WHERE" is only added once
} else {
$query .= " AND thus = '" . $value . "'";
}
}
}
HTH,
-E
So these 4 variables walk into a bar.... :^] I have a form with 4 drop downs. Each of these drop downs might have a value
and there is no order in which they might have a value. A query needs to built (the WHERE and AND portions) that takes into account which ever variables have been set. A user may choose a variable that would not be in the first location. $var1 = is not set $var2 = is set $var3 = is set $var4 = is not set (or any other combination) The query, of course, needs to be in order taking
into account the above example; $query = "SELECT foo "; $query .= "FROM bar "; $query .= "WHERE thus = '" . $var2 . "' "; $query .= "AND thus = '" . $var3 . "' "; If three of the variables are set then there would need to be an additional
AND statement, etc. It would be foolhardy to construct an if or case statement that would take into account each possible combination. I have been thinking this through for an hour or so, but I am not seeing a clear, elegant, and efficient piece of code. Has anyone done anything like this, or
does anyone have an idea of how this might be done? TIA! Jay *****************************************************_________________________________________________________________ ウィルスメール、迷惑メール対策なら MSN Hotmail http://www.hotmail.com/JA* Texas PHP Developers Conf Spring 2003 * * T Bar M Resort & Conference Center * * New Braunfels, Texas * * Contact jay.blanchard@niicommunications.com * * ** Want to present a paper or workshop? Contact now! * ***************************************************** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php