skip to Main Content

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?

2

Answers


  1. <?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.

    Login or Signup to reply.
  2. 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 AppControllers;
    
    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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search