代码之家  ›  专栏  ›  技术社区  ›  Ahmad Tahhan

编码器点火器foreach回路错误

  •  0
  • Ahmad Tahhan  · 技术社区  · 7 年前

    我得到一个 foreach 循环错误在codeigniter中,它工作得很好,但一旦我将项目移到一个新的宿主中,我就会得到一个循环错误。两台服务器都使用PHP 7.2

    以下是第一个错误:

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

    下面是第二个错误:

    Message: Invalid argument supplied for foreach()
    

    回溯说错误在控制器中,我检查了很多次,尝试了很多方法,但仍然是一样的。

    使用 is_array PHP函数没有帮助,因为它只隐藏错误消息,但函数不会像以前那样工作。

    控制器:

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

    循环代码:

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

    型号:

    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();
        }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Sinto    7 年前

    重写以下代码:

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

    作为:

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

    因为:

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

    此代码不会返回多维数组。。。你用过 row_array() 这将是一个单一的数组。

    推荐文章