简体   繁体   中英

Codeigniter foreach loop error

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM