1

How to pass multiple variables from controller to view in codeigniter?

This is my controller:

function viewmembers(){
            $this->load->model('UsersModel');
            $data['students'] = $this->UsersModel->studentlist_all();
            $data['user'] = $this->UsersModel->select_admin($id);
            $this->load->view('portal/viewstudents', $data);
    }

And here is my view:

    foreach($user as $user_select){
                            $id = $user_select['USERNAME']; 
                            $fname = $user_select['FNAME'];
                            $mname = $user_select['MNAME'];
                            $lname = $user_select['LNAME'];
                            $nick = $user_select['NICK'];
                            }
    echo $nick;

It gives me "undefined variable: nick".

I also tried print_r($user), but it gives me a value "Array()"

althought print_r($students) works

2
  • i think you have fetch result instead of row in model . Can you please post your model method select_admin($id) ?? Commented Mar 20, 2016 at 9:19
  • public function select_admin($id){ $this->db->select('*'); $this->db->from('admins'); $this->db->where('USERNAME',$id); $query = $this->db->get(); return $query->result_array(); } Commented Mar 20, 2016 at 9:20

1 Answer 1

0

result_array() will not return single row. If you want single row then you should use row_array() . Also,in your where condition you are trying to match username field against id, it is wrong .So, your method must be as below :

public function select_admin($id){
     $this->db->select('*');
     $this->db->from('admins'); 
    $this->db->where('id',$id); 
    $query = $this->db->get();
     return $query->row_array();
     }

And to echo the field value in your view :

echo $user['your_field_value_name'];

If you want to explore more then see this doc

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

4 Comments

You do not have to loop the $data['user'] value in view now. echo $user['your_filed_name'] should work buddy .
can you please post the value of print_r($data['user']); ??
Just prints as "Array ( )"
In you where condition you are doing where('USERNAME',$id); is it right ? you are trying to match username with id . I think there should be id instead of username.

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.