2014-03-04 84 views
0

嗨,我想從視圖調用模型,但我得到的錯誤爲。如何從視圖調用模型

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

我view.But tryed這下面的代碼沒有運氣提前

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; 

感謝我已經創建了一個名爲accountmodel在應用程序的模型在我結束 幫助

+0

你爲什麼不叫它從加載視圖並將變量傳遞給視圖的控制器? – IJas

+0

!!你爲什麼想要在你的視圖中調用一個模型.. –

+0

實際上有一個旅行列表名稱.in,這將是停止。我需要顯示旅行名稱列表,這是最少有一站,所以我是在foreach循環中檢查.u可以在視圖代碼 –

回答

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 

} 
?> 
+0

我改變了一點bit它的作品很好,謝謝 –

+0

有一個問題,我的問題現在得到解決..但是如果我們能夠在視圖中調用模型,代碼行會少一點..我們可以在視圖中做到這一點 –

+0

你是什麼意思? –

0

/型號dir 和加載視圖(如果您在控制器中加載模型,將會是最好的): -

$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