Welcome Guest, Not a member yet? Register   Sign In
How to pass database result set from controller to a view
#1

Hi

I am getting the following error in my View  blogview.php:

ErrorException
Undefined variable: data 
APPPATH\Views\Blogview.php at line 21

I 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();

$query = $db->query('SELECT name, title, email FROM my_table');
$data = $query->getResult();

echo view('blogview', $data);
//$this->load->view('blogview', $data);

This is the code in my View blogview.php :

Code:
<html>
   <head>
        <title>Reading from DB</title>
    </head>
<body>
   <h1>Result from DB</h1>
        <ul>
        <?php foreach ($data as $row):?>

                <li><?= $row ?></li>

        <?php endforeach;?>
        </ul>

</body>
</html>
Reply
Reply
#3

(03-28-2021, 07:34 AM)craig Wrote: https://www.codeigniter.com/user_guide/o...o-the-view
Hi 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>
Reply
#4

(This post was last modified: 03-28-2021, 12:37 PM by demyr.)

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 ??
Reply
#5

(03-28-2021, 12:36 PM)demyr Wrote: 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 ??
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();

$query = $db->query('SELECT name, title, email FROM my_table');
$data = $query->getResult();

foreach ($results as $row)
{
      echo $row->title . " ";
      echo $row->name . " ";
      echo $row->email . " ";
      echo "<br>";
}

echo 'Total Results: ' . count($results);
Reply
#6

PHP Code:
echo view('blogview', ['data' => $data]); 
Reply
#7

Let me write you an example



Model:



PHP Code:
<?php namespace App\Models;
use 
CodeIgniter\Model;


class 
CountriesModel extends Model{
    protected 
$table 'countries';
    protected $allowedFields = [
        'country_name',
        'country_name_slug',
        'country_code'
    ];
    protected $returnType 'object';
    protected $primaryKey 'country_id';

    public function get_countries(){
        $db      = \Config\Database::connect();
        $builder $db->table('countries');   
        
        $query 
$builder->select('*')
                         ->get();

        return $query->getResult();
    }

    



?>


Controller:





PHP Code:
<?php namespace App\Controllers;

use 
App\Models\CountriesModel;

class 
MyController extends BaseController
{
    public function __construct()
    {
        $this->CountryModel = new CountriesModel();
    }

public function 
countries(){

            $data['title'] = 'Countries';
            
            $data
['countries'] = $this->CountryModel->get_countries();

            echo view('folder_name/assets/header'$data);
            echo view('folder_name/countries');
            echo view('folder_name/assets/footer');
        
    
//countries ends

//basecontroller ends 




View:



PHP Code:
<ul>

  <?php foreach ($countries as $counrty) {?>
    <li><?php echo $country->country_name?></li>
  <?php }?>

</ul> 


I would advise you to keep it clean and simple.
Reply
#8

(03-28-2021, 04:50 PM)ojmichael Wrote:
PHP Code:
echo view('blogview', ['data' => $data]); 
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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB