Php 排序到最高速率的codeigniter
我有平均评分,但如何将这些评分从最高到最低排序。我的评级表只有id、restoid、userid和rate。另一张桌子是餐厅的桌子Php 排序到最高速率的codeigniter,php,codeigniter,sorting,Php,Codeigniter,Sorting,我有平均评分,但如何将这些评分从最高到最低排序。我的评级表只有id、restoid、userid和rate。另一张桌子是餐厅的桌子 这就是我获得平均速率并能够显示它的方式 重新建模(模型) 我有从高到低的那种价格 public function highToLow($cuisine) { $this->db->select("*"); $this->db->from('restaurants'); $this->db->where
这就是我获得平均速率并能够显示它的方式 重新建模(模型) 我有从高到低的那种价格
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>
-
您需要使用“加入和分组方式”来获得结果,类似于这样
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();
}
是的,谢谢你。所有的菜都很好,但我该如何把它和每一道菜搭配起来呢?就像我在上面发布的例子一样,当我点击排序价格american时,它只对american的价格进行排序代码>&别忘了接受它作为方法的参数。
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();
}