php - keep query outside of "VIEW" in codeigniter -
i quite new codeigniter.i in situation have 2 table , have 1 many relationship between them.the schema following.one employee table , other table vacation application
employee(id,name),
application(id,start_date,end_date,reason,user_id),
plan display application in single page showing start_date,end_date,reason , name
.for querying application
table have admin controller admin can query application
table.and there usercontroller data employee table.in admin model have following query applications
class adminmodel extends ci_model{ public function fetchallrequest(){ $q=$this->db->get('application'); if($q->num_rows()){ return $q->result_array(); }else{ return false; } }
admin controller following :
class admin extends ci_controller{ public function index(){ $this->load->model('adminmodel'); $data['request']=$this->adminmodel->fetchallrequest(); $this->load->view('admin/index',$data); } } }
it bring me result this:
array ( [0] => array ( [id] =5 [start] => 05/04/2016 [end] => 05/12/2016 [reason] => sick [status] => pending [employee_id] => 1
i giving single row.there can hundreds of applications .my problem need grab employee_id each row , use fetch name employee table.
one solution can go through each row in foreach loop inside view , each of them call user model passing employee id , fetch data.
if i need use user model inside "view" not mvc approach.how can solve problem.thanks !!
in ci 2: check join https://ellislab.com/codeigniter/user-guide/database/active_record.html#select
in ci 3: check join http://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data
you can join both table employee.id application.user_id
$this->db->select('*'); $this->db->from('application'); $this->db->join('employee', 'employee.id = application.user_id'); $query = $this->db->get();
Comments
Post a Comment