Received: from pompom.freestart.hu (pompom.freestart.hu [213.197.64.6]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f6VBSLf65032 for ; Tue, 31 Jul 2001 07:28:22 -0400 (EDT) (envelope-from pgerzson@freestart.hu) Received: from line-72-126.dial.freestart.hu ([213.197.72.126] helo=jaguar) by freestart.hu with smtp (Freestart relay 1 (FS-Lin)) id 15RXdW-0001mn-00; Tue, 31 Jul 2001 13:24:30 +0200 Message-ID: <021d01c119b4$2d0fc360$7e48c5d5@jaguar> From: "Gyozo Papp" To: Cc: References: <3B5779FE.74C87025@catalyst.net.nz> Subject: Re: A function for building a where clause. Date: Tue, 31 Jul 2001 09:54:48 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="Windows-1252" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 X-Scanner: exiscan by Freestart Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by postgresql.org id f6VBSMf65050 X-Archive-Number: 200107/37 X-Sequence-Number: 494 Hello, the best I can suggest to you is to gather your search conditions into an array and use some magic array function such as join. Look what I'm talking about: if(!empty($f_you)) $sort[] = "tbl_all.employee_ID = \"$f_you\""; if(!empty($f_clients)) $sort[] = "tbl_all.client_ID = \"$f_clients\""; if(!empty($f_project)) $sort[] = "tbl_all.project_ID = \"$f_project\""; if(!empty($f_task)) $sort[] = "tbl_all.task_ID = \"$f_task\"; if(!empty($f_description)) $sort[] = "tbl_all.description LIKE '$f_description'"; if(!empty($f_hours)) $sort[] = "bl_all.hours = \"$f_hours\""; if(!empty($f_date)) $sort[] = "tbl_all.date LIKE \"$f_project\""; $finalsort = join(' AND ', $sort); echo "final sort:$finalsort"; return $finalsort; If you have to use such functions in a lot of place over in your site, you may arrange a co-called controll-array which tells your function what to do with what variables. This array holds information about what variable is bound to what column of the table example (this is only a scratch not tested and not full-featured :) 'f_you', 'col' => 'employee_ID', 'rel' =>'='), /* ... other variable-column pairs for $f_clients, $f_project, $f_task ... */ array ('var' => 'f_description', 'col' => 'description', 'rel' =>' LIKE ')); array ('var' => 'f_hours', 'col' => 'hours', 'rel' => '='), /* $ctl is the control array, * $scope is the array which contains the values to be substituted * with unique keys such as variable names. ie.: $GLOBALS */ function make_where(&$ctl, &$scope) { foreach ($ctl => $row) { $value = $scope[$row['var']]; $conditions[] = $row['col'].' '.$row['rel'].' '.$value; } return 'WHERE '.join(' AND ', $conditions); } ?>