jfur7
jfur7

Reputation: 77

CodeIgniter - Pass database array from model to controller and then view

This is probably really simple, but I just started using Code Igniter.

I have the following code in a model and want to get the database query array to pass to the controller and then pass it to a page view.

public function view_user($id){
        $this->db->where('userid', $id);
        $query = $this->db->get('users');
        return $query->result_array();
}

What is the code necessary to get the returned array in the controller?

Thanks in advance for your help!

Upvotes: 0

Views: 1886

Answers (1)

Nishant Lad
Nishant Lad

Reputation: 616

here you Go with perfact answer

Model code

public function view_user($table,$id)
{
        $this->db->where('userid', $id);
        return $this->db->get($table);
}

Now in controller Call your Method Like

$this->load->model('model_name');
$data['array_name']= $this->model_name->view_user('Tabelname',$id)->result();

If u want to Get Only single row Insted of result write ->row(); in above statement

now load your view and pass the $data as 2nd pararameter

$this->load->view('viewname',$data)

now in your VIEW

Acesss Array Like

  <?php foreach($array_name as $row)?>

Upvotes: 2

Related Questions