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);
精彩评论