使用代码点火器在模型中查找每个循环问题



Model:

public function fetch_emp()   
{ 
$this->db->select('emp_id');
$this->db->from('emp');
$res=$this->db->get();
foreach($res->result() as $row)
{
$id=$row->emp_id;
$this->db->select('emp_name');
$this->db->from('employee_master');
$this->db->where('emp_id',$id);
$res=$this->db->get();
return $res->result();
}
}

控制器:

public function employee()
{
$result=$this->emp_model->fetch_emp();
if($result!=false)
$res['cmp_name']=$result;
else
$res['cmp_name']='NA';
}
$this->loadViews("emp/emp_views", $this->global, $res , NULL);
}

视图:

<?php print_r($cmp_name);?>

一旦我从 db 获取 id,我得到的所有 id 根本没有问题,但是当我在 foreach 循环中使用 id 并从另一个 db 获取他们的名字时,它只显示第一个 id 名称...... 例如:如果有 5 个 id,但是当我搜索 id 以从另一个数据库获取名称时,它只显示第一个 id 名称。我不知道有什么问题请帮助我。

你可以直接使用这样的方法完成它(获取名称列表join()

public function fetch_emp() 
{ 
$this->db->select('emp_name');
$this->db->from('emp');
$this->db->join('employee_master', 'employee_master.emp_id = emp.emp_id');
$res=$this->db->get();
return $res->result();
}

最新更新