0

I am getting a foreach loop error in codeigniter, it was working fine, but once I moved the project to a new hosting I get an error for the loop. Both servers are using PHP 7.2

Here is the first error:

Message: array_slice() expects parameter 1 to be array, null given

And here is the second error:

Message: Invalid argument supplied for foreach()

The backtrace says the error is in the controller, I have checked many times and tried many methods but still the same.

Using is_array PHP function wont help, as it only hide the error messages, but the functions wont work as before.

Controller:

public function login(){
            $data['title'] = 'Sign In';
            $this->form_validation->set_rules('email', 'Email', 'required');
            $this->form_validation->set_rules('password', 'Password', 'required');
            if($this->form_validation->run() === FALSE){
                $this->load->view('templates/header', $data);
                $this->load->view('users/login', $data);
                $this->load->view('templates/footer');
            } else {

                // Get username
                $username = $this->input->post('email');
                // Get and encrypt the password
                $password = $this->input->post('password');
                // Login user
                $user_id = $this->user_model->login($username, $password);
                if($user_id){
                    // Create session
                    $user_data = array(
                        'id' => $user_id,
                        'email' => $username,
                        'logged_in' => true
                    );
                    $this->session->set_userdata($user_data);
                    // Set message
                    $this->session->set_flashdata('user_loggedin', 'Login Success');
                    redirect('users/account');
                } else {
                    // Set message
                    $this->session->set_flashdata('login_failed', 'Login Faild');
                    redirect('users/login');
                }
            }
        }
    public function account($id = NULL){
      if(!$this->session->userdata('logged_in')){
                redirect('users/login');
            }
        $data['users'] = $this->user_model->get_users($this->session->userdata('id'));
        $data['title'] = 'Account';
        $this->load->view('templates/user_header', $data);
        $this->load->view('users/account', $data);
        $this->load->view('templates/user_footer');
    }

Loop code:

<?php foreach (array_slice($users, 0, 1) as $user): ?>
      <div><p><?php echo $user['first_name'] ?></p></div>
   <?php endforeach; ?>

Model:

public function get_users($id){
      $this->db->join('user_details', 'user_details.user_details_id = users.id');
      $this->db->join('user_orders', 'user_orders.user_id = users.id');
      $query = $this->db->get_where('users', array('id' => $id));
      return $query->row_array();
    }
6
  • show your get_users method also Commented Aug 7, 2018 at 9:35
  • How can you tell Using is_array PHP function wont help, are you sure about $users in foreach() is an array(). And, is_array() does not hides error, it prevents foreach() statement before executing & making error Commented Aug 7, 2018 at 9:41
  • @pradeep I added the get_users method Commented Aug 7, 2018 at 10:32
  • @Sinto I am sure about the $users, is_array prevent the foreach() statment, that's why adding it wont help. because if the foreach() is prevented how can I print the data on the screen Commented Aug 7, 2018 at 10:35
  • ok, i seen the query there. You do not need a foreach there because there is only only record from the query. avoid foreach & try echo $users['first_name']; Commented Aug 7, 2018 at 10:37

1 Answer 1

1

Rewrite the below code:

<?php foreach (array_slice($users, 0, 1) as $user): ?>
   <div><p><?php echo $user['first_name'] ?></p></div>
<?php endforeach; ?>

as:

<div><p><?php echo $users['first_name'] ?></p></div>

Because:

public function get_users($id){
      $this->db->join('user_details', 'user_details.user_details_id = users.id');
      $this->db->join('user_orders', 'user_orders.user_id = users.id');
      $query = $this->db->get_where('users', array('id' => $id));
      return $query->row_array();
    }

This code will not return an multi-dimension array... you have used row_array() which will be a single array.

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

1 Comment

This does NOT solve the asked question. The reported error was array_slice() expects parameter 1 to be array, null given which means that echo $users['first_name'] will also fail. Null coalescing would be required so that a default value could be displayed when there is no record for the sought id.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.