Kirubel
Kirubel

Reputation: 1627

Getting a column record from database in codeigniter

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.

Upvotes: 1

Views: 49

Answers (1)

pawan sen
pawan sen

Reputation: 726

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.

Upvotes: 1

Related Questions