Re: Sql/PHP question

From: Date: Wed, 16 Apr 2003 18:56:49 +0000
Subject: Re: Sql/PHP question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-144177@lists.php.net to get a copy of this message
On 16-Apr-2003 Mike Tuller wrote: > I had the values set for $department and $location set with the quotes. > I removed that after my posting realizing the mess I was creating. > > This could get pretty complex. I just noticed that what happens if all > is selected for both departments and locations. then you can't have > where in the query, also the and will give me a fit too if I only > select one. > > I can do it, it will just take some planning, and I will have to be > careful if I add any more selections. > function addclause($qry, $fld, $val) { if (! empty($val) ) { $qry .=sprintf(" %s $fld='%s'", ( preg_match('#\bwhere\b#mi', $qry) ? 'AND' : 'WHERE'), $val); } return($qry); } $query="SELECT ... FROM da_table "; if ($department != 'all') $query=addclause($query, 'department', $department); if ($location != 'all') $query=addclause($query, 'location', $location); Regards, -- Don Read dread@texas.net -- It's always darkest before the dawn. So if you are going to steal the neighbor's newspaper, that's the time to do it.

« previous php.general (#144177) next »