> -----Original Message-----
> From: Bastien Koert [mailto:[EMAIL PROTECTED]
> Sent: 22 September 2004 15:27
[....]
> //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
This check isn't really necessary in PHP, since $_POST['state'] will *always* be an
array if the form field has NAME='state[]', even if only 1 is selected.
> 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'])."')"; )
> }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['job'])){
> $sql .= "(";
> $x = 0;
> foreach ($_POST['job'] as $job )
> if ($x == 0){
> $sql.= "job = '$job ";
> $x=1;
> }else{
> $sql .= " OR job = '$job ";
> }
> $sql .= ")";
> }else{
> //$_POST['job'] is not an array
> $sql .= "job = '".$_POST['job']."' ";
> }//end if
Ditto for the job field.
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services, JG125, James Graham
Building, Leeds Metropolitan University, Headingley Campus, LEEDS, LS6 3QS, United
Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php