Php 将逗号分隔的字符串传递给codeigniter中的in子句
我想将逗号分隔的字符串传递给子句中的Php 将逗号分隔的字符串传递给codeigniter中的in子句,php,mysql,codeigniter,Php,Mysql,Codeigniter,我想将逗号分隔的字符串传递给子句中的。我的控制器代码如下:- $batchpost = $this->input->post('batch');//print_r($batch); $batch_id = ''; for($i=0;$i<count($batchpost);$i++ ) { $batch_id = $batch_id . $batchpost[$i] . ','; }
。我的控制器代码如下:-
$batchpost = $this->input->post('batch');//print_r($batch);
$batch_id = '';
for($i=0;$i<count($batchpost);$i++ ) {
$batch_id = $batch_id . $batchpost[$i] . ',';
}
$batch_string = rtrim($batch_id, ',');
$batch = str_replace(",", ",", $batch_string) ;
$list = $this->admin_model->get_attendance_datatables($batch);
$this->db->select('offline_student.*,batch.batch_name,offline_course.course');
$this->db->from($this->table8);
$this->db->join('offline_course', 'offline_course.id = offline_student.course', 'left');
$this->db->join('batch', 'batch.id = offline_student.batch', 'left');
$this->db->where('offline_student.status', 'Active');
$this->db->where_in('batch.id', $batch);
$this->db->order_by('offline_student.id', 'asc');
假设在数据库中有两个值(2,3)的批处理列的行中,如果我只向模型传递“2”或“2,3”,查询将返回结果,但当我只传递3时,它不会显示该记录。删除以下行:
$batch_id = '';
for($i=0;$i<count($batchpost);$i++ ) {
$batch_id = $batch_id . $batchpost[$i] . ',';
}
$batch_string = rtrim($batch_id, ',');
$batch = str_replace(",", ",", $batch_string) ;
您不需要将$batchpost
转换为逗号分隔的字符串,因为where_in()
方法在其第二个参数上需要一个数组
请参阅where_in()
的文档,如果要将其放入where_in
子句中,请将$batchpost转换为逗号分隔的字符串$batch=explode(“,”,$batch”)
通过将任何表字段名称更改为错误名称,尝试回显您的查询,并查看您的查询在SQL中的外观。这是您不需要执行的操作,只需将其作为数组传递到where_in
$batchpost = $this->input->post('batch');//print_r($batch);
$list = $this->admin_model->get_attendance_datatables($batchpost);