1

I was thinking about getting a member_id from my database Incident table which is a foreign key related to another table Member. What I wanted to get is for all member_id in the Incident table that are related to Members table so I wanted to display the Name fields of the member_id. Here is My codes are

// My Model 
function get_mid(){
    $data = array();
    $query = $this->db->get('Incident');
    foreach($query->resul_array as $row) {
        $data[] = $row['member_id'];
    }
    return $data;
}

In my Controller I have

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

That gives me all records having the same name in my view.

1 Answer 1

1

instant use code

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

replace by

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

try code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.