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


Messages In This Thread
How to pass database result set from controller to a view - by Ahirsi - 03-28-2021, 06:32 AM



Theme © iAndrew 2016 - Forum software by © MyBB