在codeigniter中的foreach中的未定义变量

时间:2018-06-05 07:09:03

标签: php codeigniter view

我是CodeIgniter的新手。我试图从数据库中显示特定用户的用户日志。我不知道发生了什么事情,它抛出了像未定义变量这样的错误。

如果我点击特定的用户编辑按钮,我的仪表板上有用户列表,它会将我带到另一个页面,我试图显示该用户的日志,但有些人认为它不起作用。

控制器:

public function userlogs() {
      if ($this->input->post('temp_user_id')) {
        $id = $this->input->post('temp_user_id');
        $this->session->set_userdata('temp_user_id', $id);
    } elseif ($this->session->userdata('temp_user_id')) {
        $id = $this->session->userdata('temp_user_id');
    }

    if ($this->session->userdata('is_logged') && $id) {
        $data['userlogs'] = $this->Org_model->getLogs($id);
        $this->load->view('template/header');
        $this->load->view("organisation/user_edit", $data);
        $this->load->view('template/footer');
    } else {
        $this->session->set_flashdata('error', 'User Id not captured.');
        redirect('Organisation/editOrg');
    }
}

控制器1:

public function editUser() {
    if ($this->input->post('temp_user_id')) {
        $id = $this->input->post('temp_user_id');
        $this->session->set_userdata('temp_user_id', $id);
    } elseif ($this->session->userdata('temp_user_id')) {
        $id = $this->session->userdata('temp_user_id');
    }

    if ($this->session->userdata('is_logged') && $id) {
        $data['userdata'] = $this->Org_model->getUsersData($id);
        $this->load->view('template/header');
        $this->load->view("organisation/user_edit", $data);
        $this->load->view('template/footer');
    } else {
        $this->session->set_flashdata('error', 'User Id not captured.');
        redirect('Organisation/editOrg');
    }
}

型号:

function getLogs($id) {
    $this->db->select('*');
    $this->db->from('log_table');
    $this->db->where('userid',$id );

    if ($query = $this->db->get()) {
         return $query->result_array();
    } else {
        return false;
    }
}

查看:

table class="table table-striped table-bordered table-hover" id="dataTable">
                        <thead>
                            <tr>
                                <th>Username</th>
                                <th>Event</th>
                                <th>Date / Time</th>
                                <th>IP Address</th>
                            </tr>
                        </thead>

                        <tbody>
                            <!-- Displaying Fetched Details from Database -->
                            <?php


                              foreach ($userlogs as $userlog) {

                                    $timestamp = $userlog->entertime;
                                    echo $timestamp;
                                    ?>

                                    <tr>

                                        <td><?php echo $userlog->userid; ?></td>
                                        <td><?php echo $userlog->log_operation; ?></td>
                                        <td><?php
                            $format = 'DATE_RFC822';
                            echo standard_date($format, $timestamp);
                                    ?></td>
                                        <td><?php echo $userlog->ip; ?></td>
                                    </tr>
                                    <?php
                                }

                            ?>    
                        </tbody>

                    </table>

但它会抛出Undefined variable: userlogs

之类的错误

我不明白我错在哪里

任何人都可以帮助我

如何解决该问题。

提前致谢。

1 个答案:

答案 0 :(得分:2)

您需要在userlogs控制器方法中添加editUser数据。尝试以下代码

public function editUser() {
    if ($this->input->post('temp_user_id')) {
        $id = $this->input->post('temp_user_id');
        $this->session->set_userdata('temp_user_id', $id);
    } elseif ($this->session->userdata('temp_user_id')) {
        $id = $this->session->userdata('temp_user_id');
    }

    if ($this->session->userdata('is_logged') && $id) {
       $data['userlogs'] = $this->Org_model->getLogs($id);
        $data['userdata'] = $this->Org_model->getUsersData($id);
        $this->load->view('template/header');
        $this->load->view("organisation/user_edit", $data);
        $this->load->view('template/footer');
    } else {
        $this->session->set_flashdata('error', 'User Id not captured.');
        redirect('Organisation/editOrg');
    }
}