How to pass database result set from controller to a view |
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> (03-28-2021, 07:34 AM)craig Wrote: https://www.codeigniter.com/user_guide/o...o-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> (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();
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. (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 |
Welcome Guest, Not a member yet? Register Sign In |