Welcome Guest, Not a member yet? Register   Sign In
how to pass query result from controller to view and display it?

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

foreach ($query->result() as $row)
echo $row->id;
echo $row->title;
echo $row->body;

the above doesn't work, need help tnx



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

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

yeah i got it to work Smile tnx a lot!

Theme © iAndrew 2016 - Forum software by © MyBB