我正在使用PHP Yii框架创建一个推荐控制器,使用启发式概念。我的建议将只工作,如果用户或其他用户有相同的课程已经预订。但是现在,如果我的预订等于null,它会显示如下错误信息:
为foreach()提供无效参数
因此,我设置了一个条件,我创建了自己的消息:
很抱歉目前没有推荐书目
,它正在工作。不幸的是,它还与错误消息一起显示。我只想展示我自己的信息。下面是我的代码:
//recommendation - use heuristic concept, more accurate result as more people same course recommendation
public function actionRecommendation() {
$user = User::model()->findByPk(Yii::app()->user->id);
//get course mates
$courseMates = User::model()->findAllByAttributes(array('course_id' => $user->course_id));
$popularity = array();
if($bookings==null){Yii::app()->user->setFlash('success', 'We are sorry currently there are no recommendation of books at this moment'); }
foreach ($courseMates as $courseMate) {
//identify all bookings made by user
$bookings = Booking::model()->findAllByAttributes(array('user_id' => $courseMate->id));
foreach ($bookings as $booking) {
//add hit into array as per book
$popularity[$booking->book_id] = (isset($popularity[$booking->book_id])) ? $popularity[$booking->book_id] + 1 : 1;
}
}
//sort according to popular count
arsort($popularity, SORT_NUMERIC);
$display = 10;
foreach ($popularity as $bookId => $popular) {
//if display is 0, break the foreach, only display up 10
if ($display == 0) {
break;
}
//create book object array
$books[] = Book::model()->findByPk($bookId);
//reduce book count
$display--;
}
$this->render('recommendation',array(
'books'=>$books,
));
}()
我如何修改它,使它只显示自定义的错误信息?
For Input Fields* Model*
array('gender','required','message'=>'Please select gender.'),
也可以调用Function
array('gender', 'ownFunction'),
public function ownFunction() {
{
if (empty($this->gender))
$this->addError("gender", 'Please select gender.');
}
}
使用这个可以在输入字段
上显示自定义错误消息For Error Page
为错误页中的自定义消息。
In your Controller
if(Your Condition)
{
//Show Error form.
throw new CHttpException(500,'We are sorry currently there are no recommendation of books at this moment. ');
}
else
{
/* rest of code goes Here */
}
希望这对你有帮助