排序到最高速率代码点火器


sort to highest rate codeigniter

我有平均评分,但如何从最高到最低对这些评分进行排序。我的评级表只有id,restoid,userid,rate。其他表是餐厅表

评分表(数据库)


这就是我获得平均费率并能够显示它的方式

重新模型(模型)

public function getAveRating($restoid)
{ 
      $where=array("restoid"=>$restoid);
      $this->db->where($where);
      $this->db->select_avg('rate');
      $query = $this->db->get('ratings')->first_row('array');
      return $query['rate'];
 }

我有从高到低的价格

public function highToLow($cuisine)
 {
    $this->db->select("*");
    $this->db->from('restaurants');
    $this->db->where('cuisine',$cuisine);
    $this->db->order_by("cost","desc");
    $result = $this->db->get()->result('array');
    return $result;
}

搜索显示(查看)

这是排序显示的页面

            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
            <!--<li><a href="<?php echo base_url()?>/index.php/HomeController/sortresto/<?= $restotype?>">A-Z<span class="sr-only">(current)</span></a></li>-->
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Price <span class="caret"></span></a>
            <ul class="dropdown-menu">
            <li><a href="<?php echo base_url()?>/index.php/HomeController/lowToHigh/<?= $restotype?>">Low to High<span class="sr-only">(current)</span></a></li>
                <li role="separator" class="divider"></li>
            <li><a href="<?php echo base_url()?>/index.php/HomeController/highToLow/<?= $restotype?>">High to Low</a></li>  
            </ul>
            </li>
            <!--<li><a href="<?php echo base_url()?>//<?= $restotype?>">Ratings</a></li>-->
          </ul>
            <ul><form class="navbar-form navbar-right" method = "POST" action = "<?php echo base_url().'HomeController/searchresto'?>">
            <div class="form-group">
            <input type="text" name="searchinfo" class="textbox" value="Restaurant or Cuisine" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Restaurant Name';}">
            </div>
                <button type="submit" class="date_btn">Search</button>
            </form></ul>
        </div>

你需要使用join&group by来获得你的结果,像这样

public function get_by_rating ($HighestFirst=TRUE) {
    $Order = $HighestFirst ? 'DESC' : 'ASC';
    $q = $this->db
            ->select('r.*')
            ->select('AVG(ra.rate) avg_rating', FALSE)
            ->from('restaurants r')
            ->join('ratings ra', 'r.id = ra.restoid', 'left')
            ->group_by('r.id')
            ->order_by('avg_rating', $Order)
            ->get();
    return $q->result_array();
}