Home > other >  CodeIgniter 4: Pass $data to the View file
CodeIgniter 4: Pass $data to the View file

Time:04-29

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

?>

CodePudding user response:

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

echo view('my_view', $data)
  • Related