I guess my problem is just about a syntax error or SORT BY should come before LIMIT, anyway after adding LIMIT to my query the following statement generates an mysql error.
$query_pag_data = "SELECT * FROM Apartment LIMIT $start, $per_page";// without LIMIT the if statement works while with LIMIT it doesn't.
if ($_GET['SortBy']=="Price" || $_GET['SortBy']=="District" ||) {
$query_pag_data .= "ORDER BY ".$_GET['SortBy']; // It doesn't work if I add LIMIT to my query
}
What is the error and how can I make this working with LIMIT and ORDER BY wout changing my logic.
Your completed SQL should read like SELECT, FROM, ORDER BY, LIMIT
. So your PHP should be written like:
$query_pag_data = "SELECT * FROM Apartment";
if ($_GET['SortBy']=="Price" || $_GET['SortBy']=="District") {
$query_pag_data .= " ORDER BY ".$_GET['SortBy'];
}
$query_pag_data .= " LIMIT $start, $per_page"
this will work
if ($_GET['SortBy']=="Price" || $_GET['SortBy']=="District") {
$query_order_by= " ORDER BY ".$_GET['SortBy'];
}
$query_pag_data = "SELECT * FROM Apartment $query_order_by LIMIT $start, $per_page";