This is easily done.

Within each of your if statements, simply call the access the select
variable and add your conditions, like so:

if(!(empty($this->getPost('customer_name')
 {
  $select->where('customer_name = ?', $this->getPost('customer_name');
} 

You can add as many pieces to your $select before executing the
statement.

Hope that helps!




-----Original Message-----
From: SalamFall [mailto:[email protected]] 
Sent: Sunday, December 21, 2008 6:18 AM
To: [email protected]
Subject: [fw-general] Build a conditional Query


Thanks in advance for any help on this.
I want to build a Conditional query for example.

$package = new Package()

$select = $package->select()->from('packages')->join('customers',
'customers.id = packages.pid)
               ->where('packages.id = ?', 347)->order('recdate DESC');

I want to be able to add to this query based on conditions, for example
if 
customer name is filled in the search form add search customer name to
this
query as in 

if(!(empty($this->getPost('customer_name')
 {
  // if customer name is provided in the search form then add that
search to
the query
 } 

Basically I am trying to reproduce a conditional query like it is done
when
u are not using a framework like this

$query = 'Select username, first_name, last_name From table ';
if(!(empty($_POST["date_of_birth"]))
 {
   $query .= ' WHERE date_of_birth=\'$_POST["date_of_birth"] \' ';
}
if(!(empty($_POST["email"]))
 {
   $query .= ' WHERE email=\'$_POST["email"] \' ';

}
and so on, but with the zend framework.

I hoe it's not confusing.

Thanks in advance
-- 
View this message in context:
http://www.nabble.com/Build-a-conditional-Query-tp21114184p21114184.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to