Data is not showing on view page in Codeigniter 4

Note: It was working on single view page, but once I pass it to User Dashboard it will be showing error.

I was trying to pass user data from database to form table but it was showing error

Here's my code:

 public function questionList() {

        $list = new QuestionsModel();

        $data['questionList'] = $list->findAll();

        return view('user/dashboard', $data);
}

Note that I had already have my QuestionsModel created

Here's my dashboard.php section of the code

<table class="table">
                    <thead>
                        <tr>
                            <th>User ID</th>
                            <th>Lists</th>
                            <th>Answered</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        
                    <?php foreach($questionList as $list) { ?>
                        
                        <tr>
                            <td><?= $list['user_id'] ?></td>
                            <td><?= $list['list'] ?></td>
                            <td><?= $list['answered'] ?></td>
                            <td><?= $list['status'] ?></td>

                        </tr>

                        <?php } ?>

                    </tbody>
                </table>

How can I solve this please?

Upvotes: 0

Views: 580

Answers (3)

This function. In good very codeigniter 4:

enter image description here

Upvotes: 0

Digvijaysinh chauhan
Digvijaysinh chauhan

Reputation: 74

I hope this will works for you : still face issue add comment so i can help more.

My route :

$routes->get('/rules', 'Home::rules');

My Controller :

<?php

namespace App\Controllers;

class Home extends BaseController
{

    public function rules()
    {   
        $data['test'] = 'ABC';
        return view('pages/rules', $data);
    }
}

My View File :

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
</head>
<body>
  <span><?php echo $test; ?></span>
</body>
</html>

My Output :

enter image description here

Upvotes: 1

LemonFridge
LemonFridge

Reputation: 61

<?php foreach($data['questionList'] as $list) { ?>
<tr>
    <td><?= $list['user_id'] ?></td>
    <td><?= $list['list'] ?></td>
    <td><?= $list['answered'] ?></td>
    <td><?= $list['status'] ?></td>
</tr>

You don't have a key in the foreach loop. By adding $data before accessing $questionList, you are explicitly telling the view to look for the $questionList variable in the $data array that you passed to the view.

Upvotes: 0

Related Questions