pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Timothy_Maguire@hartehanks.com
To: anepon@verveinternet.com
Cc: pgsql-php@postgresql.org
Cc: pgsql-php-owner@postgresql.org
Subject: Re: A function for building a where clause.
Date: Thu, 19 Jul 2001 13:09:53 -0400
Message-ID: <OFDC68B869.791D9EB6-ON85256A8E.005D28F9@hartehanks.com> (raw)


I would not include the WHERE and AND statements in your snippet here.
Take all the "table.field = '$some_var'" and push them into an array and
then create the query on the fly and plug the WHERE and AND as you go.

for example,
$query_array = array();
if (!empty($f_you))
{
     array_push($query_array, "tbl_all.employee_id = '$f_you'");
}
if (!empty($next_var))
{
     array_push($query_array, "tbl_all.somefield = '$some_var'");
}
...and so on.....

then take that array and plug the WHERE and AND in.

$query = "SELECT * FROM tbl_all WHERE ";
for($i=0;$i<sizeof($query_array);$i++)
{
     $query .= $query_array[$i];
     if($i < sizeof($query_array) - 1)
     {
          // this will plug the AND into the $query inbetween all the array
elements except for the last one
          $query .= " AND ";
     }
}

There could be a better way to do this.  I would be interested in any other
solutions.

Tim.

Timothy P. Maguire
Web Developer II
Harte-Hanks
978 436 3325



                                                                                                                     
                    "Ari Nepon"                                                                                      
                    <anepon@verveinterne       To:     <pgsql-php@postgresql.org>                                    
                    t.com>                     cc:                                                                   
                    Sent by:                   Subject:     A function for building a where clause.                  
                    pgsql-php-owner@post                                                                             
                    gresql.org                                                                                       
                                                                                                                     
                                                                                                                     
                    07/19/01 12:46 PM                                                                                
                    Please respond to                                                                                
                    anepon                                                                                           
                                                                                                                     
                                                                                                                     




Does anyone know of a function that I could use to take form data and build
a where clause for my SQL query based on their choices (which can be from
text fields, memo fields, dropdowns of numbers or letters, and radio
buttons
or check boxes).

Here is something I wrote, but i know is not correct, and would fail unless
the first choice is chosen. But it gives an idea of what I am trying to do.

function searchparams()
{
global $f_you;
global $f_clients;
global $f_project;
global $f_task;
global $f_description;
global $f_hours;
global $f_date;

     if(!empty($f_you))
     $sort1="WHERE tbl_all.employee_ID = \"$f_you\"";
     if(!empty($f_clients))
     $sort2="AND tbl_all.client_ID = \"$f_clients\"";
     if(!empty($f_project))
     $sort3="AND tbl_all.project_ID = \"$f_project\"";
     if(!empty($f_task))
     $sort4="AND tbl_all.task_ID = \"$f_task\";
     if(!empty($f_description))
     $sort5="AND tbl_all.description LIKE '$f_description'";
     if(!empty($f_hours))
     $sort6="AND  bl_all.hours = \"$f_hours\"";
     if(!empty($f_date))
     $sort7="AND tbl_all.date LIKE \"$f_project\"";
     $finalsort="$sort1  $sort2  $sort3  $sort4  $sort5  $sort6  $sort7";
     echo "final sort:$finalort $f_project";
     return $finalsort;

}

Problem with this function is that I am getting a where clause that has AND
sometable.somecolumn="" and its screwing up my query.

Thanks,

Ari


~~~~~~~~~~~~~~~~~~~~~~~~~
Ari Nepon
MRB Communications
4520 Wilde Street, Ste. 2
Philadelphia, PA 19127
p: 215.508.4920
f: 215.508.4590
http://www.mrbcomm.com

-----------------------------------------------------------------------
Sign up for our email list and receive free information about
topics of interest to nonprofit communications, marketing, and
community building professionals. Free resources, articles, tips.
Go to http://www.mrbcomm.com and use the Mailing List form.
-----------------------------------------------------------------------


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly






view thread (4+ messages)  latest in thread

Message-ID: <OFDC68B869.791D9EB6-ON85256A8E.005D28F9@hartehanks.com>
Permalink:  ../OFDC68B869.791D9EB6-ON85256A8E.005D28F9@hartehanks.com/
Also on:    postgresql.org/message-id/OFDC68B869.791D9EB6-ON85256A8E.005D28F9@hartehanks.com

 · 

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: Timothy_Maguire@hartehanks.com, anepon@verveinternet.com, pgsql-php-owner@postgresql.org
  Subject: Re: A function for building a where clause.
  In-Reply-To: <OFDC68B869.791D9EB6-ON85256A8E.005D28F9@hartehanks.com>
  References: <OFDC68B869.791D9EB6-ON85256A8E.005D28F9@hartehanks.com>

* 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