Skip to content
Advertisement

CodeIgniter 4: Pass $data to the View file

Can someone help me with this? I can’t figure out on how can I show the results on the View file. I tried using $this->load->view('list-logs',$data); but it shows the error Call to a member function view() on null.

CONTROLLER

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

    }

VIEW

<?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>';
    }
}

?>

Advertisement

Answer

if you use CodeIgniter 4 then you must call the view ont controller like this:

echo view('my_view', $data)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement