pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Gyozo Papp <pgerzson@freestart.hu>
To: anepon@verveinternet.com
Cc: pgsql-php@postgresql.org
Subject: Re: A function for building a where clause.
Date: Tue, 31 Jul 2001 09:54:48 +0200
Message-ID: <021d01c119b4$2d0fc360$7e48c5d5@jaguar> (raw)
References: <JMEEKPBGBOBMKNANAGILKECACEAA.anepon@verveinternet.com>
	<3B5779FE.74C87025@catalyst.net.nz>

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);
}
?>




view thread (4+ messages)

Message-ID: <021d01c119b4$2d0fc360$7e48c5d5@jaguar>
Permalink:  ../021d01c119b4$2d0fc360$7e48c5d5@jaguar/
Also on:    postgresql.org/message-id/021d01c119b4$2d0fc360$7e48c5d5@jaguar

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: pgerzson@freestart.hu, anepon@verveinternet.com
  Subject: Re: A function for building a where clause.
  In-Reply-To: <021d01c119b4$2d0fc360$7e48c5d5@jaguar>
  References: <JMEEKPBGBOBMKNANAGILKECACEAA.anepon@verveinternet.com> <3B5779FE.74C87025@catalyst.net.nz> <021d01c119b4$2d0fc360$7e48c5d5@jaguar>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox