Passage of variables to the view in CodeIgniter

4

I'm finding some problems to pass the result of a query in the model to the view in CodeIgniter. What would be the best way to do this?

My query: $query = $this->db->query("SELECT * FROM crm.usuario limit 50");

And I want to make a listing with the results in the view.

    
asked by anonymous 19.03.2015 / 17:52

2 answers

5

To send controller information to the view in codeigniter you can do this:

Controller.php

 $data['usuarios'] = $this->db->get('usuarios')->result();
 $this->load->view('listagem_usuarios', $data); // carrega a view e envia a variável $data

In your view access the list of users by calling the array key $data

foreach($usuarios as $item){ 
  echo $item['nome'] .'<br>';
}

Recommended reading:

CI - Views

CI - Active Record Class

CI - Queries

    
19.03.2015 / 19:39
0

Oops, good night, is it possible to do that? When it puts so the error does not find the data in the foreach, I do not have control  $ data ['users'] = $ this-> db-> get ('users') -> result ();

$ datatab ['table'] = $ this-> db-> get ('table') -> result ();  $ this- > load- > view ('user_list', $ data, $ datatab);

    
18.06.2017 / 06:30