Reputation: 857
I have a problem with sorting some data in my page. Here is the snippet:
<form name=frm action="" method="post" style="display: inherit;">
<label>
Эрэмбэ:
<select class="input-select" name="qryorder" onchange="document.frm.submit()">
<option value="asc">Price ascending</option>
<option value="desc">Price descending</option>
</select>
</label>
</form>
Here is the data i want to sort:
Code snippet:
<?php
if(isset($_POST['qryorder'])) {
$sort = $_POST['qryorder'];
sort_by_price($sort);
// header('Location: product.php');
}
?>
And my function:
function sort_by_price($sort)
{
global $conn;
$sql = "select * from product order by price = '{$sort}'";
$result = $conn->query($sql);
$list = array();
while ($row = $result->fetch_assoc()) {
$list[] = $row;
}
return $list;
}
What i exactly want is i need to sort some data ordered by price ascending and descending.
Upvotes: 0
Views: 63
Reputation: 6541
Don't use =
operator in you order by clause.
Reference: Sorting Rows
$sql = "select * from product order by price $sort";
Updated Code: Price field in table was VARCHAR which was causing incorrect sorting, converted into BIGINT.
Upvotes: 1