phpcodeigniterwhere-clausequery-builderwhere-in

how to use array in where clause in Codeigniter


I'm using codeigniter, trying to use an array in where clause and return only those id match with array.

Submitted by form:

$arrTID = $this->input->post('Present'); print_r($arrTID);

output:

Array(
 [0]=>FMM003
 [1]=>30089
 [2]=>30097
)

When use in foreach:

foreach($arrTID as $key=>$ID){               
 print_r($ID);
}

output: FMM0033008930097

query:

$query = $this->db->query("select TraineeID from tbl_attendance_processed where TraineeID IN $ID and attnDate='$atnDate'");
$res=$query->result_array();

I'm actually need that query return those ID in array which match with this query. How do get this?


Solution

  • $arrTID = $this->input->post('Present');
    $atnDate = $this->input->post('atnDate');
    
    $this->db->select('TraineeID');
    $this->db->where_in('TraineeID', $arrTID);
    $this->db->where('attnDate', $atnDate);
    $query = $this->db->get('tbl_attendance_processed');
    
    // and fetch result
    $res = $query->result(); // as object
    $res = $query->result_array(); // as array
    

    and learn this http://www.codeigniter.com/userguide2/database/active_record.html