【问题标题】:Codeigniter user_model Invalid argument supplied for foreach()Codeigniter user_model 为 foreach() 提供的参数无效
【发布时间】:2015-06-25 08:21:16
【问题描述】:

我的 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 有什么问题?

【问题讨论】:

  • var_dump($roles) 看看你得到了什么。
  • 您要在会话中分配什么?
  • @b0s3 在哪里放置var_dump($roles)...
  • $roles = $this->db......之后。
  • 它显示文本:NULL

标签: php codeigniter foreach


【解决方案1】:

尝试在foreach() 之前检查count($roles)

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

【讨论】:

  • 不起作用,使用 count($roles) 会消失错误,但是登录部分我再也看不到了...
  • print_r($roles); 之前的 if 条件并将结果粘贴到此处。
  • A PHP Error was encountered Severity: Notice Message: Undefined variable: roles Filename: models/user_model.php Line Number: 15
  • 这意味着roles 变量没有保存任何数据。
  • 我能做些什么来解决这个问题?!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多