0

I am writing a query that will retrieve nodes that match a set of criteria. Though the number of cirteria is unknown. I was hopping i could pass to the condition() method an array of criteria with a key as the 'column name' and that value as the 'value to match' for that colum. For instance :

$criteria = array ('nid' => 1, 'etid' => '1')

$query = db_select('table', 't');

$query
  ->condition( $criteria)
  ->fields('t', some_field);

I have almost 10 criteria and no way to know which will be selected so writing a query for each wont be possible.

Is there a way to achieve that ?

EDIT:

i thought about using that :

if (empty($criteria1)){
    $criteria = array (":criteria1"= '*');
}else {
 $criteria = array (":criteria1"=$criteria1);
}
$query = db_query('SELECT nid FROM { parce_index } WHERE criteria1 = :criteria1 AND criteria2= criteria2...',$criteria));

Would that make sense ?

0

If anyone ever need that i got an answer there.

http://drupal.org/node/12843

This is useful as well :

https://stackoverflow.com/questions/7476611/how-to-write-a-query-with-a-dynamic-where-clause

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.