我在business_items表的视图中,我正在获取id并反对此id我正在检索data.Below是我在Business Items控制器中的功能。
public function actionTest()
{
$this->layout='main';
$modelitems = BusinessItems::model()->findAll(); //getting all data of businessitems
$rate = ItemReview::model()->findAll(); //getting all data of businessitems
$this->render(
'test',
array(
'rate' => $rate,
'modelitems' => $modelitems,
)
);
}
这里是我的视图文件,我称之为测试。我希望获得平均评分和业务ID。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
$value = $_POST["business_id"];
$matchFound = false;
$ratematch = false;
$save = 0;
foreach ($modelitems as $ba) {
$bizitems = $ba->id; //getting id of business items
$biz = $ba->business_id; //business id from model business items
if ($value == $biz) { //matching posted id of business with ids in business items table
$image = $ba->image; //geting image from business items table
$item_name = $ba->items->item_name; //geting item name from business items table
foreach ($rate as $ab) {
$ratebiz = $ab->business_items_id; //getting business items id from item review
if ($ratebiz == $bizitems) { //comparing business items id with business items id in item review
echo "business items id:" . $bizitems;echo " ";
echo "rating of business items" . $ab->rating;
echo "<br.>";
}
}
}
}
我的输出就像这个商业项目ID:52商业项目评级4
business items id:52 rating of business items5
business items id:52 rating of business items5
business items id:52 rating of business items3
business items id:52 rating of business items3
business items id:53 rating of business items2
business items id:53 rating of business items5
business items id:54 rating of business items2
business items id:54 rating of business items1
business items id:54 rating of business items4
business items id:54 rating of business items3
business items id:55 rating of business items5
business items id:55 rating of business items4
business items id:55 rating of business items3
business items id:55 rating of business items2
我希望我的输出像这样
business items id:52 rating of business items20 //20 is the total rating
business items id:53 rating of business items7 //7 is the total rating
business items id:54 rating of business items10 //10 is total rating
business items id:55 rating of business items14 //14 is total rating
我该怎么办?
答案 0 :(得分:0)
为每个循环添加以下sum变量并修改循环如下:
$sum=0;
foreach ($rate as $ab){
$ratebiz=$ab->business_items_id; //getting business items id from item review
if($ratebiz==$bizitems){ //comparing business items id with business items id in item review
$sum=$sum+$ab->rating;
}
}
echo "business items id:".$bizitems;echo " "; echo "rating of business items".$sum;
echo "</br>";