Am trying to use order by date and price low to high in this statement
SELECT * FROM allpostdata WHERE sts = '1' AND mca='Vehicle' ORDER BY pdt DESC, prs ASC
i used echo $query; to get the query and here is how i did,
looking for ORDER BY pdt DESC, is default query i want prs to be user option .
when i select order by low to high or high to low the statement changes but query does nothing, its not sorting by price
How do i sort by prs any solution?
pdt means Date and prs means Price
HTML
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="sortby">
<li class="dropdown-item">
<div class="md-radio my-1">
<input type="radio" class="filter_all sort" name="sort" id="asc" value="ASC">
<label for="asc">Price : Low to High</label>
</div>
</li>
<li class="dropdown-item">
<div class="md-radio my-1">
<input type="radio" class="filter_all sort" name="sort" id="desc" value="DESC">
<label for="desc">Price : High to Low</label>
</div>
</li>
</div>
Script
$(document).ready(function () {
filter_data();
function filter_data() {
$.post(
"fetch.php",
{
action: 'fetch_data',
cate: get_filter('cate'),
brand: get_filter('brand'),
model: get_filter('model'),
sort: get_filter('sort'),
date: get_filter('date')
}
)
.done(function (data) {
$('.filter_data').html(data);
});
}
function get_filter(class_name) {
var filter = [];
$('.' + class_name + ':checked').each(function () {
filter.push($(this).val());
});
return filter;
}
$('.filter_all').click(function () {
filter_data();
});
});
PHP
if (isset($_POST["action"])) {
$query = "SELECT * FROM allpostdata WHERE sts = '1' AND mca='Vehicle'";
if (!empty($_POST['cate'])) {
$query .= " AND sca IN (" . str_repeat("?,", count($_POST['cate']) - 1) . "?)";
} else {
$_POST['cate'] = []; // in case it is not set
}
if (!empty($_POST['brand'])) {
$query .= " AND product_brand IN (" . str_repeat("?,", count($_POST['brand']) - 1) . "?)";
} else {
$_POST['brand'] = []; // in case it is not set
}
if (!empty($_POST['model'])) {
$query .= " AND mdl IN (" . str_repeat("?,", count($_POST['model']) - 1) . "?)";
} else {
$_POST['model'] = []; // in case it is not set
}
$query .= " ORDER BY pdt DESC";
if (!empty($_POST['sort'])) {
if ($_POST["sort"][0] == "ASC" || $_POST["sort"][0] == "DESC") { //simplistic whitelist
$query .= ", prs " . $_POST['sort'][0];
}
}
echo $query;
$stmt = $conn->prepare($query);
$params = array_merge($_POST['cate'], $_POST['brand'], $_POST['model']);
$stmt->execute($params);
$result = $stmt->fetchAll();
$total_row = $stmt->rowCount();
$output = '';
pdt? You realize that the query as you have it will sort on that field first, andprswill only be a tie-breaker between records wherepdtis the same?pdtisproduct dateandprsispriceORDER BY pdt DESCwhenpriceis triggered.