suresh
suresh

Reputation: 439

undefined variable in foreach in codeigniter

all I am new to CodeIgniter. I am trying to show the user logs from the database for the particular user. I don't know what happened it throwing an error like an Undefined variable.

I have users list on my dashboard if I click on particular user edit button it will take me to another page where I am trying to show that user logs but some point it's not working.

Controller:

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

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

Model:

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

View:

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>

But it throwing error like Undefined variable: userlogs

i didn't understand where i did mistake

Can anyone help me

How to resolve that issue.

Thanks in advance.

Upvotes: 0

Views: 157

Answers (1)

B. Desai
B. Desai

Reputation: 16436

You need to add userlogs data in editUser controller method. try below code

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

Upvotes: 2

Related Questions