2014-05-07 58 views
0

我正在嘗試訪問模型中的函數,在我的視圖中codeigniter和它的不工作。 有人可以告訴我問題在哪裏。調用視圖中的模型函數

MODEL:

function gettemplates() 
    { 
     $sql = "select * from srs_templates"; 
     $query = $this->db->query($sql); 
     $result = $query->result_array(); 
     echo "<pre>"; print_r($result);exit; 
     return $result; 
    } 

VIEW:

<select id="special" > 
    <?php echo $this->dashboard_ext_model->gettemplates(); ?> 
</select> 
+0

爲什麼要訪問模型?檢查模型是否已經加載。 –

+0

告訴使用錯誤你得到了什麼? – Manwal

+0

正如你可以看到@Manwal我試圖在模型函數的第四行上打印取出的值,但輸出是完全空白頁面,是的,我已經加載模型。 –

回答

0

更改此:

echo "<pre>"; print_r($result);exit; 

要這樣:

echo "<pre>"; print_r($result); echo "</pre>"; 

基本上刪除出口。退出中止腳本。

您從來沒有理由從視圖調用模型。模型數據應該傳遞給控制器​​,然後傳遞給視圖。

0

由於這是MVC(模型視圖控制器),您不應該從視圖中調用模型。 您應該在控制器中調用模型,然後作爲參數傳遞給視圖。

型號:

function gettemplates() 
{ 
    $sql = "select * from srs_templates"; 
    $query = $this->db->query($sql); 
    $result = $query->result_array(); 
    echo "<pre>"; print_r($result);exit; 
    return $result; 
} 

控制器:

function gettemplates(){ 

    //$this->dashboard_ext_model = $this->load->model('dashboard_ext_model'); 
    //uncomment this if you didn't load the model 

    $data['templates'] = $this->dashboard_ext_model->gettemplates(); 

    $this->view->load('page_name', $data); 
} 

查看:

<select id="special" > 
    <?php echo $templates ?> 
</select> 

MVC可以看第一愚蠢的,但它確實有助於在大尺寸的項目。

MVC DESIGN:http://i.stack.imgur.com/Beh3a.png

相關問題