Answer the question
In order to leave comments, you need to log in
What is the best way to do this kind of sorting in yii2?
What is the best way to do this sorting?
<form class="form-inline order-by">
<div class="form-group">
<label>Sort by:</label>
</div>
<div class="form-group">
<select class="form-control">
<option selected="selected">Default</option>
<option>Popularity</option>
<option>Average rating</option>
<option>Newness</option>
<option>Price: low to high</option>
<option>Price: high to low</option>
</select>
</div>
</form>
<option value="http://localhost/mysite/frontend/web/index.php/catalog/index?sort=-view_count">
Popularity
</option>
Answer the question
In order to leave comments, you need to log in
General principle:
<form action="">
<select name="order_by">
<option value=""></option>
<option value="view_count">По популярности</option>
</select>
</form>
class NewsSearch extends Model {
public $order_by;
public function rules(){
return [
['order_by', 'in', 'range' => ['view_count', 'created_at']],
];
}
public function search(){
// ...
$query->orderBy([$this->order_by => SORT_ASC]);
// ...
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question