Re: A function for building a where clause.

From: "Gyozo Papp" <pgerzson(at)freestart(dot)hu>
To: <anepon(at)verveinternet(dot)com>
Cc: <pgsql-php(at)postgresql(dot)org>
Subject: Re: A function for building a where clause.
Date: 2001-07-31 07:54:48
Message-ID: 021d01c119b4$2d0fc360$7e48c5d5@jaguar
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-php

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 :)
<?php
$search = array(
array ('var' => '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);
}
?>

In response to

Browse pgsql-php by date

  From Date Subject
Next Message Hunter, Ray 2001-08-08 23:30:50 RE: user authentications
Previous Message Grant 2001-07-30 23:53:39 Postgresql + Sessions + Large objects