我有平均评级,但如何从最高到最低排序。我的评级表只有id,restoid,userid,rate。其他桌子是餐馆桌子
Ratings table(database)
这就是我获得平均费率并能够显示它的方式
RestoModel(模型)
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;
}
searchDisplay(视图)
这是排序显示的页面
<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>
答案 0 :(得分:0)
您需要使用加入&amp;分组来获得你的结果,像这样的东西
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();
}