如何使用foreach检查条件

时间:2015-10-07 00:54:33

标签: php codeigniter

我想检查一个主题是否有超过三名教师。所以我试图从表格中选择一些条件的ID。事实是我有一两个以上的课程和指数。那么如何检查所有类和索引?我无法成功,怎么可能,我不熟悉CodeIgniter

控制器

$checkr =  $this->class_model->check($class_data,$subject);

模型

function check($class_data,$subject)
  {
     foreach($class_data as $key => $value)
    {   
     $this->db->select('s_id');
     $this->db->from('table1');
     $this->db->where('type',2);
     $this->db->where('subject',$subject);
     $this->db->where('status',1); 

        $condition= array(
                    'class'    => $key,
                    'stream_index'     => $value 
                   );
        $this->db->where($condition);  
    }

   $rows = $this->db->get();
   $count= $rows->num_rows() ;
   return $count
}

2 个答案:

答案 0 :(得分:0)

我认为这会对你有帮助..

function check($class_data,$subject){
    $class_teacher_count = array();
    foreach($class_data as $key => $value){   
        $this->db->select('s_id');
        $this->db->from('table1');
        $this->db->where('type',2);
        $this->db->where('subject',$subject);
        $this->db->where('status',1); 

        $this->db->where('class', $key);
        $this->db->where('stream_index',$value);

        $query = $this->db->get();
        $rows = $query->result();
        $count = count($rows);
        $class_teacher_count[$key] = $count;
    }
    return $class_teacher_count;
}

答案 1 :(得分:0)

function check($class_data,$subject){
    foreach($class_data as $key => $value){   
        $this->db->select('s_id');
        $conditions = array(
            'type' => 2,
            'subject' => $subject,
            'status' => 1,
            'class' => $key,
            'stream_index' => $value 
        );
        $teacher_count[$key] = $this->db->get_where('table1',$conditions)->num_rows();

    }
   return $teacher_count;
}