Madhu Nair
Madhu Nair

Reputation: 436

Loop over query in MySQL

Drupal 7 DB select

Here I am getting a array:


Array ( [71] => adad [85] => erer )

How can I loop the above array in this query below?

function get values($array){
 $q =  db_select('node', 'n');
  $q->join('ab', 'f', 'n.nid = f.entity_id');
  $rooms_data = $q->condition('n.type', 'book');
if($room_selected!=null)
{
  $rooms_data = $q->condition('n.nid', $array); **//assumption//**
}
   $rooms_data = $q->fields('n', array('nid', 'title'))
    ->fields('f', array('feesid'))
    ->orderBy('n.title', 'ASC')
    ->execute();
}

Assuming something like:

->wherein($array as $a)
{
$q->condition('n.nid',$a['value']);
}

Upvotes: 0

Views: 50

Answers (1)

Syscall
Syscall

Reputation: 19764

You could use "IN" as $operator of SelectQuery::condition, and use an array of scalar values as $value. To do this, you could use array_column() to extract these values:

function get_values($array, $room_selected) {
    $q =  db_select('node', 'n');
    $q->join('ab', 'f', 'n.nid = f.entity_id');
    $q->condition('n.type', 'book');
    if ($room_selected != null)
    {
        $values = array_column($array, 'value') ;
        $q->condition('n.nid', $values, 'IN') ;
    }
    $rooms_data = $q->fields('n', array('nid', 'title'))
        ->fields('f', array('feesid'))
        ->orderBy('n.title', 'ASC')
        ->execute();
}

Upvotes: 2

Related Questions