2016-01-08 74 views
1

我是一個可愛的人,我想從我的「NgoRateReviewNgo」表中獲得前5名。前5名Ngo的條件是,它應該首先找到Ngo的平均評分,然後顯示那些平均評分最高的5個Ngo。這是我的查看文件的代碼,幷包含SQL查詢Yii:以最高平均評分爲基礎獲取數據

<div class="main"> 
    <div class="banner"> 
<img src="<?php echo Yii::app()->request->baseurl;?>/img/stat1.jpg" style="width: 1170px"> 
</div><!--banner ending here--><br> 
    <div class="container" style="background-color:#7AC5CD"> 
     <div class="row"> 
      <div class="col-md-6"> 
     <h3 style='text-align:center; text-decoration: underline;font-family: sans-serif; color: black'>Top 5 Ngo's month wise</h3> 
     <br> 
    <?php 
    for($month = 1 ; $month <=12 ; $month++) 
    { 
     '<br>' ; 
     $dateObj = DateTime::createFromFormat('!m', $month); 
     $monthName = $dateObj->format('F'); 
     echo "<h3 style='text-align:center;color:black;'> " . $monthName . "</h3>"; 

     $user=UserRateReviewNgo::model()->findAll(array(
     'condition' => 'YEAR(date_created)=:year and MONTH(date_created)=:month', 
     'params' => array(':year'=>2016, ':month'=>$month), 
     'select'=>'max(rate),ngo_id', 
     'group' => 'ngo_id', 
    'order'=>'rate DESC', 
    'limit' => 5 
    )); 
     $val = 100; 
     foreach($user as $show) { 

      $model = Ngo::model()->findByAttributes(array('id'=>$show->ngo_id,)); 
      if (isset($model)) { 
       echo "<div><h4><a href='".Yii::app()->request->baseUrl."/ngo/ngopage?id=$model->id'><p style='color:black;font-family:Lucida Sans Unicode;'>" . $model->ngo_name ."</p></a></h4></div> 


        <div class='progress'> 
        <div class='progress-bar progress-bar-danger progress-bar-striped active' role='progressbar' 
        aria-valuenow='" . $val ."' aria-valuemin='0' aria-valuemax='100' style='width: ". $val ."%;'>" . $val . 
        "</div> 
        </div>"; 
        $val = $val -21.23; 


    } }} 

    ?> 
     </div> 

      <div class="col-md-5 col-md-offset-1"> 
       <h3 style='text-align:center; text-decoration: underline;font-family: sans-serif; color: black'>Top 5 Ngo's of the year</h3> 
<br> 
<?php // the for the year 
    $val = 100; 
     foreach($userYear as $show) 
      { 
      $model = Ngo::model()->findByAttributes(array('id'=>$show->ngo_id,)); 
      if (isset($model)) 
       { 
       echo "<div><h4><a href='".Yii::app()->request->baseUrl."/ngo/ngopage?id=$model->id'><p style='color:black; font-family:Lucida Sans Unicode;'>" . $model->ngo_name ."</p></a></h4></div> 


        <div class='progress'> 
        <div class='progress-bar progress-bar-success progress-bar-striped active' role='progressbar' 
        aria-valuenow='" . $val ."' aria-valuemin='0' aria-valuemax='100' style='width: ". $val ."%;'>" . $val . 
        "</div> 
        </div>"; 
        $val = $val -17.96; 

      } 

     } 
    ?> 
</div> 
      </div> 
     </div><!--main ending here--> 

</div><!--Container ending here--> 

請幫我這個。

+0

只是一個小問題..我好像記得,我已經做了這種類型的查詢在最近幾天。今天的要求有什麼不同? – scaisEdge

+0

這是最大的評價,但這個職位是關於平均評級。 –

+0

回覆該代碼plase,所以我可以快速更改..請.. – scaisEdge

回答

1

這應該是查詢

$user=UserRateReviewNgo::model()->findAll(array(
    'condition' => 'YEAR(date_created)=:year and MONTH(date_created)=:month', 
    'params' => array(':year'=>2016, ':month'=>$month), 
    'select'=>'avg(rate),ngo_id', 
    'group' => 'ngo_id', 
'order'=>'rate DESC', 
'limit' => 5 
)); 
+0

好的謝謝夥計,我會盡力讓你知道。 –

+0

在任何情況下,如果你需要改變其他查詢與最大平均得到..絕對變化最大(yourField)與avg(yourField)都是組功能和工作方式相同.. – scaisEdge

+0

@SalikAsad你沒有說是否答案是正確的...... – scaisEdge