Pass data from library to controller and then to view in CodeIgniter 2

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 04:40:19

问题


I'm trying to pass some data from my custom library to the controller and make it available in my view. For the life of me, I can't see where I'm going wrong.

I was going to get database results; for testing I've resorted to a simple array but it's still not working.

The library:

class Test{
  public function __construct(){
    $this->CI =& get_instance();
  }
  public function getStuff(){
    $test = array('one','two','three');
    return $test;
  }
}

The controller:

function __construct(){
  parent::__construct();
  $this->load->library(array('form_validation', 'test'));
}

public function index(){
  $data['q'] = $this->test->getStuff();
  $this->load->view('index', $data);
}

The view:

var_dump($q); // returns Message: Undefined variable: q

It's late, so this could be a stupid error/typo. This is using CodeIgniter 2.1.0. I've Googled this - none of the solutions seem to work.

Thanks for taking a look!

来源:https://stackoverflow.com/questions/10134312/pass-data-from-library-to-controller-and-then-to-view-in-codeigniter-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!