Re: Building a query on multiple variables, how to test for which variables are valid?

From: Date: Wed, 28 Aug 2002 23:23:20 +0000
Subject: Re: Building a query on multiple variables, how to test for which variables are valid?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-114433@lists.php.net to get a copy of this message
In article <000201c24ec3$d884db50$8102a8c0@000347D72515>, jay.blanchard@niicommunications.com says... > 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 One possibility: make an array of the $varn that are set count the array elements if 0 elemet - error handling build basic query // $query = "Select foo from bar WHERE x = $element[0]" if >1 element, add additional AND by looping through balance of array Code is left as an exercise for the student :-) Cheers -- David Robley Temporary Kiwi! Quod subigo farinam

« previous php.general (#114433) next »