CodeIgniter 4:将 $data 传递给视图文件

CodeIgniter 4: Pass $data to the View file

有人可以帮我解决这个问题吗?我不知道如何在视图文件中显示结果。我尝试使用 $this->load->view('list-logs',$data); 但它显示错误 Call to a member function view() on null.

控制器

public function getLogs()
    {
        helper(['form']);

        $EstMod = new EstMod();
        $data['est_data'] = $EstMod->orderBy('e_id', 'DESC')->findAll();

        $rep_est = $this->request->getPost('rep_est');
        $rep_date = $this->request->getPost('rep_date');

        $builder = $this->db->table("logs as l");
            $builder->select('l.l_id, u.fname, u.mname, u.lname, u.dept, u.user_type, u.u_grdsec, e.e_name, l.date_added, DATE(l.date_added), date_format(l.date_added, "%H:%i") as "time"');
            $builder->join('users as u', 'l.u_id = u.u_id');
            $builder->join('establishment as e', 'l.e_id = e.e_id');
            $builder->where('DATE(l.date_added)', $rep_date);
            $builder->groupBy('date_format(l.date_added, "%H:%i"), l.u_id');
            $data['logs_data'] = $builder->get()->getResult();

        $this->load->view('list-logs',$data);
        //echo "<pre>";
        //print_r($data);

    }

查看

<?php


if ($logs_data) {
    foreach ($logs_data as $row) {
        
        echo '
        <tr>
          <td>' . $row['date_added'] . '</td>
          <td>' . $row['lname'] . ", " . $row['fname'] . ", " . $row['mname'] . '</td>';
        
        if ($row['user_type'] == 'S') {
            echo '<td>STUDENT</td>';
        } else if ($row['user_type'] == 'E') {
            echo '<td>EMPLOYEE</td>';
        } else if ($row['user_type'] == 'P') {
            echo '<td>VISITOR</td>';
        }
        
        if ($row['dept'] == 'GS') {
            echo '<td>GRADE SCHOOL</td>';
        } else if ($row['dept'] == 'JHS') {
            echo '<td>JUNIOR HIGH SCHOOL</td>';
        } else if ($row['dept'] == 'SHS') {
            echo '<td>SENIOR HIGH SCHOOL</td>';
        }
        
        echo '</td>
        <td>' . $row['u_grdsec'] . '</td>';
    }
}

?>

如果您使用 CodeIgniter 4,那么您必须像这样调用控制器上的视图:

echo view('my_view', $data)