CodeIgniter Forums
how to pass query result from controller to view and display it? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: how to pass query result from controller to view and display it? (/showthread.php?tid=43567)



how to pass query result from controller to view and display it? - El Forum - 07-16-2011

[eluser]Alpha[/eluser]
hi im new, i have $data & $query object, how to display my query object in my view file?

$data['keywords'] = "keys";
$data['description'] = "descript";

$query = $this->db->query('SELECT id, title, body FROM my_table');

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

in my view file

<?php
foreach ($query->result() as $row)
{
echo $row->id;
echo $row->title;
echo $row->body;
}?>

the above doesn't work, need help tnx


how to pass query result from controller to view and display it? - El Forum - 07-16-2011

[eluser]jmadsen[/eluser]
http://ellislab.com/codeigniter/user-guide/general/views.html

enjoy


how to pass query result from controller to view and display it? - El Forum - 07-16-2011

[eluser]Alpha[/eluser]
tnx, i've read that. what i am planning is to prepare the data through sql query at the controller side then pass it to view rather than the view file directly doing the sql query and displaying it


how to pass query result from controller to view and display it? - El Forum - 07-16-2011

[eluser]jmadsen[/eluser]
Based on that answer, you need to read it a few more times

MVC involves using a model to collect your data, calling it in a controller to prepare it for the view, and finally displaying it in the view.

You will do yourself a big favor by reading the basic tutorials over and over until that idea is something you really understand and can imitate on your own


how to pass query result from controller to view and display it? - El Forum - 07-16-2011

[eluser]Alpha[/eluser]
yeah i got it to work Smile tnx a lot!