我有功能,它可以提高教师的平均费率,并保存变量$result
的平均值我希望将变量传递给get_teacher_average_rating
并打印平均值,但我尝试这样做我有错误
注意(8):未定义的变量:结果 [APP / Controller / RatingSchoolsController.php,第30行]警告(2): 无法修改标头信息 - 已经发送的标头(输出 开始于 /Applications/MAMP/htdocs/classera-core-code/lib/Cake/Utility/Debugger.php:795) [APP / Controller / AppController.php,第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 :(得分: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);
}