How to combine HTML Select with Switch clause
I want to create a "Sort by" list in which any list item is selected, then the larvel php switch statement will return with the correct request. How should I do it?
I still have
<select class="form-control" name="SortbyList" >
<option value="1">Highest Avg</option>
<option value="2">Lowest Avg</option>
<option value="3">Another Sort option</option>
<option value="2">another sort option</option>
</select>
How can I use this with a switch that will be in my Laravel controller?
+3
source to share
3 answers
I have it all but nothing happens in terms of selection and switching? here is all the code i have ...
<select class="form-control" name="SortbyList" >
<option value="1">Highest Avg</option>
<option value="2">Lowest Avg</option>
<option value="3">Another Sort option</option>
<option value="2">another sort option</option>
</select>
And the controller:
$productsQuery = Product::where('approved', '=', 1)->leftJoin('reviews', 'reviews.products_id', '=', 'products.id')->select('products.*', DB::raw('AVG(ratings) as ratings_average' ))->groupBy('products.id');
switch ($request->SortbyList) {
case 1:
$productsQuery = $productsQuery->orderBy('ratings_average', 'DESC');
break;
case 2:
$productsQuery = $productsQuery->orderBy('ratings_average', 'ASC');
break;
case 3:
$productsQuery = $productsQuery->orderBy('ratings_average', 'ASC');
break;
case 4:
$productsQuery = $productsQuery->orderBy('ratings_average', 'ASC');
break;
default:
$productsQuery = $productsQuery->orderBy('ratings_average', 'DESC');
}
$name=$request->input('productname');
$count=$request->input('country_id');
if(!empty($name)){
$productsQuery->where('productname', 'LIKE', '%'.$name.'%')->get();
}
if(!empty($count)){
$ProductsQuery->where('country_id', $request->input('country_id') )->get();
}
$products= $ProductsQuery->paginate(10);
I'm sorry 3 and 4 don't code them yet.
0
source to share
You don't need to handle sorting with PHP, you can do it client side using JS / jQuery. This will work without refreshing the page.
<script>
jQuery( document ).ready(function() {
jQuery(".form-control").change(function(){
var conceptName = jQuery('#link').find(":selected").text();
if(conceptName == "Highest Avg"){
//sorting code here for highest
}
if(conceptName == "Lowest Avg"){
// code here
}
if(conceptName == "Another Sort option"){
// code here
}
});
});
</script>
-1
source to share