我正在考虑从我的数据库事件表中获取member_id,这是与另一个表成员相关的外键。 我想得到的是Incident表中与Members表相关的所有member_id,因此我想显示member_id的Name字段。这是我的代码
// My Model
function get_mid(){
$data = array();
$query = $this->db->get('Incident');
foreach($query->resul_array as $row) {
$data[] = $row['member_id'];
}
return $data;
}
在我的控制器中我有
public function index() {
$member_id = $this->incident_model->get_mid();
if(count($member_id)){
foreach($member_id as $id){
$f_name =$this->incident_model->get_fname($id);
$m_name =$this->incident_model->get_mname($id);
$l_name =$this->incident_model->get_lname($id);
$data = array(
'first_name' => $f_name,
'middle_name' => $m_name,
'last_name' => $l_name
);
}
$this->load->view('incident_list', $data);
}
}
这给了我视图中所有具有相同名称的记录。
答案 0 :(得分:1)
即时使用代码
public function index(){
$member_id = $this->incident_model->get_mid();
if(count($member_id)){
foreach($member_id as $id){
$f_name =$this->incident_model->get_fname($id);
$m_name =$this->incident_model->get_mname($id);
$l_name =$this->incident_model->get_lname($id);
$data = array(
'first_name' => $f_name,
'middle_name' => $m_name,
'last_name' => $l_name
);
}
$this->load->view('incident_list', $data);
}
替换为
public function index(){
$member_id = $this->incident_model->get_mid();
$temp = array();
$data = array();
if(count($member_id)){
foreach($member_id as $id){
$f_name =$this->incident_model->get_fname($id);
$m_name =$this->incident_model->get_mname($id);
$l_name =$this->incident_model->get_lname($id);
$temp = array(
'first_name' => $f_name,
'middle_name' => $m_name,
'last_name' => $l_name
);
$data[] = $temp;
}
$this->load->view('incident_list', $data);
}
尝试代码。