簡體   English   中英

Codeigniter user_model為foreach()提供的參數無效

[英]Codeigniter user_model Invalid argument supplied for foreach()

我有user_model.php問題,以下是錯誤:

遇到PHP錯誤嚴重性:警告

消息:為foreach()提供了無效的參數

文件名:models / user_model.php

行號:18

回溯:

文件:\\ httpdocs \\ application \\ models \\ user_model.php行:18函數:_error_handler

文件:\\ httpdocs \\ application \\ controllers \\ user.php行:9功能:check_role

文件:\\ httpdocs \\ index.php行:292功能:require_once

user_model.php

public function check_role()
{
    $user_id = $this->session->userdata('admin_user_id');
    // get roles
    if ($user_id) {
        $row = $this->db->get_where(TBL_USERS, array('id' => $user_id))->row();
        $roles = $this->db->get_where(TBL_ROLES, array('id' => $row->role_id))->row_array();
        foreach ($roles as $key => $value) {
            $this->session->set_userdata($key, $value);
        }
    }
}

foreach有什么問題?

嘗試在foreach()之前檢查count($roles) foreach()

public function check_role()
{
    $user_id = $this->session->userdata('admin_user_id');
    // get roles
    if ($user_id) {
        $row = $this->db->get_where(TBL_USERS, array('id' => $user_id))->row();
        $roles = $this->db->get_where(TBL_ROLES, array('id' => $row->role_id))->row_array();
        if(count($roles)>0)
        {
            foreach ($roles as $key => $value) {
               $this->session->set_userdata($key, $value);
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM