Ticket to view in Codeigniter

Asked

Viewed 4,615 times

4

I am finding some problems to pass the result of a query present in the model, to the view in Codeigniter. What would be the best way to do that?

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

And I want to list the results in the view.

  • From what you said I believe you use a model based on MVC. Are you using any framework? Or have you created the structure yourself?

  • In fact the manure has already been delivered ready to me, I was only responsible for making some changes...

  • How do you render views? What do you call them?

  • I’m sorry, I don’t understand your question... As I mentioned before, I entered this branch of programming a short time ago, and kind of directly, without taking specific courses or anything...

  • Okay... I’ll try to improve the question, you are probably doing this query exactly where the executions take place to render the screen to where you want to pass this query right? Could supplement the code with the rest of the programming you have after your $query?

  • My query is within this function present in the model ---- public Function get_online_users() { $query = $this->db->select('*') ->limit('50') ->get('usuarios.crm') ->result(); }

  • Dude it’s hard to help without knowing the rest of the scope... whether or not a framework is used... if you use a.... they implement different ways to send information to the view...

  • 1

    The structure is from Codeigniter, which uses MVC

  • Remember to put tech/frameworks tags on your question so it’s easier for those who will answer or search. :)

  • @Okay, thanks a brother!

Show 5 more comments

3 answers

5


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

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 go to 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

  • Thanks brother! Solved!

0

opa good night, is it possible to do that? qdo put so error does not find the data in the foreach, in the controller I have $data['usuarios'] = $this->db->get('usuarios')->result();

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

-1

Pass only one date parameter:

$this->load->view('listagem_usuarios', $data);

In your view:

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

Or:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.