简体   繁体   中英

Pass multiple variables from controller to view in codeigniter

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM