3

I loop in a loop and there are errors. I tried this before but now it doesn't work.

My errors are:


A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: views/audit_trail_view.php

Line Number: 36

Backtrace:

File: C:\xampp\htdocs\cemo-marikina\application\views\audit_trail_view.php Line: 36 Function: _error_handler

File: C:\xampp\htdocs\cemo-marikina\application\controllers\Employee.php Line: 2819 Function: view

File: C:\xampp\htdocs\cemo-marikina\index.php Line: 292 Function: require_once


A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'user_id'

Filename: views/audit_trail_view.php

Line Number: 36

Backtrace:

File: C:\xampp\htdocs\cemo-marikina\application\views\audit_trail_view.php Line: 36 Function: _error_handler

File: C:\xampp\htdocs\cemo-marikina\application\controllers\Employee.php Line: 2819 Function: view

File: C:\xampp\htdocs\cemo-marikina\index.php Line: 292 Function: require_once

Views: audit_trail_view.php

<?php 
    $first_name='';
    $last_name='';
    $middle_name='';
    $separator="";
    $user_id="";

    ?>
        <table border ="1" width="75%" >
            <tr>
                <td>User</td>
                <td>Action</td>
                <td>Date</td>
            </tr>
    <?php
    foreach($view as $view){

        $audit_id= $view['audit_id'];
        $audit_user_id= $view['audit_user_id'];
        $audit_record= $view['audit_record'];
        $audit_new_record= $view['audit_new_record'];

        $audit_date= $view['audit_date'];
        $timestamp=strtotime($view['audit_date']);


        $year=date('Y', $timestamp);
        $month=date('M', $timestamp);
        $day=date('d', $timestamp);
        $hour=date('h', $timestamp);
        $minute=date('i', $timestamp);
        $ap=date('a', $timestamp);
        foreach($users as $users){
            $id =$users['user_id'];
            if($id==$audit_id){
                $first_name=$users['fname'];
                $last_name=$users['lname'];
                $middle_name=$users['mname'];
            }
            else{
                $first_name='';
                $last_name='';
                $middle_name='';
            }

        }
    ?>  


        <tr>

            <td><?php echo $last_name." ".$first_name.", ".$middle_name;?> </td>
            <td><?php echo$audit_record.$separator.$audit_new_record;?> </td>
            <td><?php echo$month." ".$day.", ".$year." ".$hour.":".$minute." ".$ap;?> </td>
        </tr>   

    <?php   
    }





?>
    </table>

Controller:Employee.php

public function audit_trail_view(){
    $data['view']=$this->emp_model->audit_trail_view();
    $data['users']=$this->emp_model->audit_trail_users();
    $this->load->view("audit_trail_view.php",$data);
}

Model: Emp_model.php

/* AUDIT TRAIL VIEW START*/
    function audit_trail_view(){
        $this->db->select('*');
        $this->db->from('audit_table');

        $query = $this->db->get();
        return $query->result_array();
    }

    function audit_trail_users(){
        $this->db->select('*');
        $this->db->from('users');
        $query = $this->db->get();
        return $query->result_array();
    }
/* AUDIT TRAIL VIEW END*/

/* AUDIT TRAIL START */
    function audit_trail(){
        $data=array(
            'audit_user_id'=>$this->input->post('audit_user_id'),
            'audit_record'=>$this->input->post('audit_record'),
            'audit_new_record'=>$this->input->post('audit_new_record'),
        );
        $this->db->insert('audit_table',$data);
    }
/* AUDIT TRAIL END */   
1
  • $id =$users['user_id']; - @Abdulla Commented Feb 24, 2016 at 18:37

2 Answers 2

3

Your for each seems odd:

  foreach($users as $users){
        $id =$users['user_id'];
         ....

Shouldn't it be more like:

  foreach($users as $user){
        $id =$user['user_id'];
        .....

I'm sure iterating and assigning to the same variable name is probably resulting in an unexpected array.

Sign up to request clarification or add additional context in comments.

Comments

3

You are getting this Uninitialized string offset notice becuase of your foreach value, you can not use same variable name for values otherwise it will ve treated as overwrite.

Change these:

foreach($view as $view_value){
     $audit_id = $view_value['audit_id'];
     .......
}

foreach($users as $user_value){ 
      $id = $user_value['user_id'];
      .....
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.