2017-08-09 80 views
0

函數我有教師平均速度的函數,我保存變量的平均值$result我想將變量傳遞給get_teacher_average_rating並打印平均值,但我嘗試這樣做,我有錯誤通過函數cakephp 2

通知(8):未定義變量:導致 [APP /控制器/ RatingSchoolsController.php 30行]警告(2): 無法修改標題信息 - 頭已經發出(輸出 開始在 /應用/MAMP/htdocs/classera-core-code/lib/Cake/Utility/Debugger.php:795) [APP/Controller/AppController.php,li NE 67]

我怎麼能解決這個問題,並打印平均get_teacher_average_rating

public function set_teacher_average_rating($result) 
    { 
     $this->autoRender = false; 
     if($this->request->is('get')) 
     { 
      $author_id = $this->request->query('author_id');// 
      $rate = $this->Rating->find('all',array('conditions'=>array('author_id'=> $author_id), 'fields'=> array('AVG(Rating.value) as averageRating'), 'recursive' =>-1)); 

      $average = $rate[0][0]['averageRating']; 
      $result = array('success'=>'1' , 'average' => $average); 
     } 
     else{ 
      $result = array('success'=>'0','message'=>'request type is not GET'); 
     } 
     echo json_encode($result); 
    } 


public function get_teacher_average_rating() 
    { 
     $this->autoRender = false; 
       $average = $this->set_teacher_average_rating($result); 
       $rate = $this->Rating->find('all',array('conditions'=>array('author_id'=> $author_id))); 

        //get rate teacher 
           echo json_encode($average); 



     } 
+0

'$ average = $ this-> get_teacher_average_rating($ result);'__What__是'$ result'在這裏? –

+0

$ result在get_teacher_average_rating中來自哪裏?從視圖?會話? –

+0

那麼'get_teacher_average_rating'調用'get_teacher_average_rating'?你確定你做對了嗎? –

回答

0

試試這個如果函數set_teacher_average_rating()不被任何其他函數調用:

public function set_teacher_average_rating() 
    { 
     $this->autoRender = false; 
     if($this->request->is('get')) 
     { 
      $author_id = $this->request->query('author_id');// 
      $rate = $this->Rating->find('all',array('conditions'=>array('author_id'=> $author_id), 'fields'=> array('AVG(Rating.value) as averageRating'), 'recursive' =>-1)); 

      $average = $rate[0][0]['averageRating']; 
      $result = array('success'=>'1' , 'average' => $average); 
     } 
     else{ 
      $result = array('success'=>'0','message'=>'request type is not GET'); 
     } 
     return $result; 
    } 


public function get_teacher_average_rating() 
    { 
     $this->autoRender = false; 
       $average = $this->set_teacher_average_rating(); 
       $rate = $this->Rating->find('all',array('conditions'=>array('author_id'=> $author_id))); 

        //get rate teacher 
           echo json_encode($average); 



     } 
+0

我嘗試你的代碼,但顯示錯誤信息「允許的內存大小134217728字節用盡」 – ahad

+0

然後修復此錯誤。有很多例子來修復這個錯誤在互聯網上 –