drxm72811 2016-06-11 22:31
浏览 82

视图CI中的未定义变量

Users.php Controller

class Users extends CI_Controller{
  public function userList(){
    $data['results'] = $this->user_model->get_users();
    $this->load->view('users_view',$data);
  }
}

user_model.php Model

class User_model extends CI_Model{
  public function get_users()  
  {  
    $query = $this->db->get('users');  
    return $query->result();  
  }
}

users_view.php View

foreach ($results as $list) {
  echo $list->id;
}

main.php View

$this->load->view('users_view');

When i view http://localhost:81/rokki/users/userlist the id is displayed. But, when i view in http://localhost:81/rokki/home thru the main.php I'm getting errors as below :

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: results

Filename: views/users_view.php

Line Number: 41

Backtrace:

File: C:\xampp\htdocsokki\application\views\users_view.php Line: 41 Function: _error_handler

File: C:\xampp\htdocsokki\application\views\layouts\main.php Line: 20 Function: view

File: C:\xampp\htdocsokki\application\controllers\Home.php Line: 7 Function: view

File: C:\xampp\htdocsokki\index.php Line: 315 Function: require_once

I'm newbie to the CI. Please help me. Thanks in advance

  • 写回答

2条回答 默认 最新

  • duancai1904 2016-06-11 22:49
    关注

    Do you have a controller for home like you do for users? Looking at your call stack it appears you have one so I assume it's something like this if it isn't already.

    class Home extends CI_Controller{
        public function index(){
            $data['results'] = $this->user_model->get_users();
            $this->load->view('main',$data);
       }
    }
    

    Then in your main.php passing the results data to the view_users view like so.

    $this->load->view('view_users', $results);
    
    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致