从Controller传递数组到codeigniter中查看

时间:2015-09-07 12:09:27

标签: php mongodb

嗨,我是mongodb和codeigniter的新手,我试着做一个简单的CRUD操作 mongodb和php codeigniter我试图将我的数组从Controller传递到视图,但它无法完成它给出了一个错误 遇到PHP错误

严重性:注意

消息:未定义的变量:user_list

文件名:views / show_user.php

行号:15

这是我的Controller代码

class user extends CI_Controller
{
    public function __construct()
    {
         parent::__construct();
         //loading  the mongodb library
         $this->load->model('mongo_model');
    }

   //index where the controller starts
    public function index() 
    {
        $collection = $this->mongo_model->selectCollectiondb();
        $collection= iterator_to_array($collection);
        foreach ($collection as $users)
        {
        $data['_id']=$users['_id'];
        $data['firstname']=$users['firstname'];
        $data['lastname']=$users['lastname'];
        $data['email']=$users['email'];
        $data['password']=$users['password'];
        }
        print_r($data);
        $this->load->view('show_user',$data);
    }
}

视图代码是

<?php 
print_r($user_list);
exit();
<?php }?>

2 个答案:

答案 0 :(得分:0)

控制器代码中没有定义任何$ user_list变量。

在您的视图中打印$ data。

  <?php 
   print_r($data);
   exit();
  ?>

答案 1 :(得分:0)

您没有将正确的数组传递给变量来查看。

CodeIgniter以父数组的形式将变量/数组从控制器传递到视图。

在您的情况下,$data

$data的密钥将成为视图中附加$符号的变量。

实际上,在观看次数中,$dataextract()

更正后的代码:

public function index()  {
  $collection = $this->mongo_model->selectCollectiondb();
  $collection= iterator_to_array($collection);
  $user_list = array();
  foreach ($collection as $users) {
    $data['_id']=$users['_id'];
    $data['firstname']=$users['firstname'];
    $data['lastname']=$users['lastname'];
    $data['email']=$users['email'];
    $data['password']=$users['password'];
    $user_list[$users['_id']]['firstname'] = $users['firstname'];
    $user_list[$users['_id']]['lastname'] = $users['lastname'];
    $user_list[$users['_id']]['email'] = $users['email'];
    $user_list[$users['_id']]['password'] = $users['password'];
  }
  $data['user_list'] = $user_list;
  print_r($data);
  $this->load->view('show_user',$data);
}