2011-09-19 36 views
0

在我的控制器中,我調用了一個模型,它使得mysql查詢從表中獲取信息。它工作正常,但現在在同一個函數下,我想調用另一個模型,並基於之前的mysql查詢的結果之一進行查詢。 (我想得到結果的字段名是「批」)。我試圖在我的控制器中獲取值(批處理),將其傳遞到模型中,然後嘗試進行第二個查詢,但似乎第二個模型沒有從控制器獲取值,因此無法工作。你能否幫我解決這個問題? 感謝提前:)如何從控制器傳遞模型中的一個mysql查詢結果?

這裏是我的控制器

 function Get($id){ 
       $this->load->model('mod_studentprofile'); 
       $data['query']= $this->mod_studentprofile->student_get($id); 

       // To get the batch name 
       $batch= $query ['batch']; // This I get from the above query result. 

       $this->load->model('batchname'); 
       $data['query1']= $this->batchname->batchname($batch); 

       $data['tab'] = "Student Profile"; 
       $data['main_content']='studentprofile'; 
       $this->load->view('includes/template',$data); 

      } 

這是我的型號1

function student_get($id) 
    { 
     $query=$this->db->get_where('student',array('studentid'=>$id)); 


     return $query->row_array(); 

    } 

這是我的型號2

function batchname($batch) 
    { 
     $query1=$this->db->get_where('batch',array('batchid'=>$batch)); 

     return $query1->row_array(); 
    } 

回答

0

嗯,你實際上並沒有給$批處理分配任何東西,這個怎麼樣:

$batch= $data['query']; 

現在這個參數傳遞給變量。作爲一個側面說明,你可以把它作爲一個paramater和與它在一個單一的線完成,依賴注入風格:

$data['query1']= $this->batchname->batchname($this->mod_studentprofile->student_get($id)); 
0

你是第一個查詢得到的值回?

我會在控制器中記錄您從第一個查詢中獲得的值。

log_message('debug', 'Batch value is '.$batch); 

而且還進行了一些調試,以查看正在運行的查詢的內容。

function batchname($batch) 
{ 
     $query1=$this->db->get_where('batch',array('batchid'=>$batch)); 
     $str = $this->db->last_query(); 
     log_message('debug', 'Batchname Query: '.$str); 
     return $query1->row_array(); 
} 
相關問題