Re: A function for building a where clause. - Mailing list pgsql-php

From Gyozo Papp
Subject Re: A function for building a where clause.
Date
Msg-id 021d01c119b4$2d0fc360$7e48c5d5@jaguar
Whole thread Raw
In response to A function for building a where clause.  ("Ari Nepon" <anepon@verveinternet.com>)
List 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
asjoin. 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
tellsyour function what to do with what variables. This array holds information about what variable is bound to what
columnof 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);
}
?>


pgsql-php by date:

Previous
From: Grant
Date:
Subject: Postgresql + Sessions + Large objects
Next
From: "Hunter, Ray"
Date:
Subject: pg_connect vs. pg_pconnect