如何从视图中调用模型

时间:2014-03-04 06:28:03

标签: php codeigniter view model

您好我正在尝试从视图中调用模型,但我收到错误。

A PHP Error was encountered Severity: Notice Message: Undefined property: 
CI_Loader::$Stopsmap_Model Filename: stopsmap/index.php Line Number: 59

我在视图中尝试了以下代码。但没有运气

foreach ($routes as $route_details):
//calling model

//$trip_max_min_sequence_details = $this->load->Stopsmap_Model->fullTripdetails($trips->trip_id); i tried this also but same error
//$this->load->model('Stopsmap_Model');
//$data = $this->stopsmap_model->fullTripdetails($route_details->route_id);  i tried this also but same error
$this->load->model('fixedtransit_model/Stopsmap_Model');
$get_route_related_trips = $this->Stopsmap_Model->getRoutetrips($route_details->route_id);
  if(count($get_route_related_trips)>=1):
    echo '1 stop';
  endif;
endforeach;

提前感谢您的帮助

2 个答案:

答案 0 :(得分:1)

在您的控制器中,您可以这样:

function routes(){
  //call model
  $this->load->model('fixedtransit_model/Stopsmap_Model');

  $routes = $this->topsmap_model->get_routes();

  foreach ($routes as $route_details){

   $get_route_related_trips[] = $this->Stopsmap_Model->getRoutetrips($route_details->route_id);

  }

  $data['related_routes'] = $get_route_related_trips;

  //load view and pass data
  $this->load->view('myview' , $data);

}

并在您看来:

<?php foreach($related_routes){

//do something

}
?>

答案 1 :(得分:0)

在我的结尾 我在applications / models目录中创建了一个名为 accountmodel 的模型 并在视图中加载(如果你在控制器中加载模型,那将是最好的): -

$this->load->model('AccountModel','Account');
//AccountModel (model class name)
//Account (object) you can further use this model using object

了解更多信息: - http://ellislab.com/codeigniter/user-guide/general/models.html