【问题标题】:I just want to print my output once我只想打印一次我的输出
【发布时间】:2015-11-15 22:31:07
【问题描述】:

我在 business_items 表的视图中,我在其中获取 id 并针对此 id 我正在检索数据。以下是我在 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,            
        )
    );
}

这是我的视图文件,我称之为 test.我想在其中获得平均评分和商家 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 业务项目评级

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

我该怎么办?

【问题讨论】:

  • 你只是要求输出之间的换行符吗?...

标签: php yii foreach output views


【解决方案1】:

在每个循环之前添加以下 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>";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多