简体   繁体   中英

Yii: Getting data on the basis of highest average rating

I am a yiibie and I want to get the top 5 ngo's from my "NgoRateReviewNgo" table. The condition for the top 5 Ngo's is that it should first find the average rating of the Ngo's and after that those 5 Ngo's which have highest average rating gets displayed. This is the code for my view file and contains the SQL query

  <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--> 

Please help me with this.

this should be the query

 $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
 )); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM