How to pass database result set from controller to a view - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: How to pass database result set from controller to a view (/showthread.php?tid=78922) |
How to pass database result set from controller to a view - Ahirsi - 03-28-2021 Hi I am getting the following error in my View blogview.php: ErrorException Undefined variable: data
APPPATH\Views\Blogview.php at line 21I have tried the following in my Controller but I am still getting the error: 1. echo view('blogview', $data); 2. $this->load->view('blogview', $data); This is the code in my Blog.php Controller Code: $db = \Config\Database::connect(); This is the code in my View blogview.php : Code: <html> RE: How to pass database result set from controller to a view - craig - 03-28-2021 https://www.codeigniter.com/user_guide/outgoing/views.html#adding-dynamic-data-to-the-view RE: How to pass database result set from controller to a view - Ahirsi - 03-28-2021 (03-28-2021, 07:34 AM)craig Wrote: https://www.codeigniter.com/user_guide/outgoing/views.html#adding-dynamic-data-to-the-viewHi Craig: Many thanks for the link. I have read it again and even added the following: echo view('BlogView', $results, ['saveData' => true]); I am unable to display the database result set in my View blogview. Getting the same error: Undefined variable results in the forach line: <ul> <?php foreach ($results as $item):?> <-------- Undefined variable results <li><?= $item ?></li> <?php endforeach;?> </ul> RE: How to pass database result set from controller to a view - demyr - 03-28-2021 Firstly, please try to make your query works in Models. Secondly : Code: <li><?= $row ?></li> $row what? $row->user_name ?? or $row->phone_number ?? or $row->blog_title ?? RE: How to pass database result set from controller to a view - Ahirsi - 03-28-2021 (03-28-2021, 12:36 PM)demyr Wrote: Firstly, please try to make your query works in Models.Hi demyr: The issue is passing the database result set to the view. The following code works just fine in the Controller: Code: $db = \Config\Database::connect(); RE: How to pass database result set from controller to a view - ojmichael - 03-28-2021 PHP Code: echo view('blogview', ['data' => $data]); RE: How to pass database result set from controller to a view - demyr - 03-28-2021 Let me write you an example Model: PHP Code: <?php namespace App\Models; Controller: PHP Code: <?php namespace App\Controllers; View: PHP Code: <ul> I would advise you to keep it clean and simple. RE: How to pass database result set from controller to a view - Ahirsi - 03-28-2021 (03-28-2021, 04:50 PM)ojmichael Wrote:Hi ojmichael: That single line fixed my issues. Thank you so much for all your help. The CI community is awesome. With my limited knowledge I want to give back where I can: documentation or any place there is a need. Who do I contact to volunteer for tasks? I would like to see CI grow to be the best and most popular framework in order to honor Jim and his team. I have been trying out CI for the first time in less than a week and I love it. AHirsi |