Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将逗号分隔的字符串传递给codeigniter中的in子句_Php_Mysql_Codeigniter - Fatal编程技术网

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);