RE: [PHP-DB] MultSelect ListBox hell!

From: Date: Wed, 22 Sep 2004 17:44:43 +0000
Subject: RE: [PHP-DB] MultSelect ListBox hell!
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-36345@lists.php.net to get a copy of this message
Just to confirm, This is what I'm going to start with: //base sql statement $sql = "select * from jobs where record_deleted = 'NO' "; if (isset($_POST['states'])){ //check to see if the states is an array // multiple items or just one if (is_array($_POST['state'])) $sql .= "state='".implode("' OR state='", $_POST['state'])."'"; }else{ //$_POST['state'] is not an array $sql .= "state = '".$_POST['state']."' "; }//end if if (isset($_POST['job'])){ if (isset($_POST['state'])){ $sql .= " AND "; } //add in the AND if the state is set //check to see if the states is an array //multiple items or just one if (is_array($_POST['state'])) $sql .= "state='".implode("' OR state='", $_POST['state'])."'"; $sql .= ")"; }else{ $_POST['job'] is not an array $sql .= "job = '".$_POST['job']."' "; } //end if ?> --- "Ford, Mike" <M.Ford@leedsmet.ac.uk> wrote: > > -----Original Message----- > > From: Stuart Felenstein > [mailto:stuart4m@yahoo.com] > > Sent: 22 September 2004 16:45 > > > > --- "Ford, Mike" <M.Ford@leedsmet.ac.uk> wrote: > > > > > > if (is_array($_POST['state'])){ > > > > $sql .= "("; > > > > $x = 0; > > > > foreach ($_POST['state'] as $state) > > > > if ($x == 0){ > > > > $sql.= "state = '$state' "; > > > > $x=1; > > > > }else{ > > > > $sql .= " OR state = '$state' "; > > > > } > > > > $sql .= ")"; > > > > > > PHP has the very nice implode function to make > this > > > much easier: > > > > > > $sql = "state='".implode(' OR state=', > $_POST['state'])."'"; > > > > > > (If your database supports the IN operator, this > is > > > probably even better: > > > $sql = "state IN ('".implode("','", > $_POST['state'])."')"; ) > > > > Sorry, cause I know this is probably a stupid > > question, but what block of code does the implode > > statement replace ? > > Everything inside the if(is_array()). Ummm, that > should probably still be a > .= operator, then. There's also some quotes missing > in the original > version, so: > > if (is_array($_POST['state'])) > $sql .= "state='".implode("' OR state='", > $_POST['state'])."'"; > > Cheers! > > Mike > >

« previous php.db (#36345) next »