开发者

How to display the contents of a return array from model to view in codeigniter

开发者 https://www.devze.com 2023-04-06 12:16 出处:网络
How to display the contents of a return array from model to view 开发者_高级运维in codeigniter?

How to display the contents of a return array from model to view 开发者_高级运维in codeigniter?

Code: model

   $arr_data[] =$query2;
   return $arr_data[];

how to display the array contents in view page using codeigniter


You get data from model, specify an array key element on controller, then while loading view you specify that array as second parameter. And in view, that key becomes your variable.

Sample model:

Class Your_model Extends CI_Model {

    function get_stuff() {
        //do stuff
        $stuff[0] = 'data1';
        $stuff[1] = 'data2';
        return $stuff;
    }

}

Sample Controller:

Class Your_controller Extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function public_controller() {
        $this->load->model('your_model');

        $output['model'] = $this->your_model->get_stuff();
        $putput['otherstuff'] = 'other stuff';

        $this->load->view('your_view',$output);

    }

}

Sample view:

foreach ($model as $eachmodel) {
    ?><b><?php echo $eachmodel; ?></b><?php
}


replace "return $arr_data[];" with "return $arr_data", the assign the variable to the view.

$this->load->view('viewName', array('modeldata' => $arr_data);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号