我正在使用带有php的ajax来排序和显示我的数据库。但是我不能在asc和desc之间切换。它只适用于ASC,虽然我不知道为什么。无论我按下按钮多少次,$ isAsc都保持在1。 (我使用的是PHP 7.1) // AJAX
$('#sort_by_name').click(function(e){
e.preventDefault();
$.ajax({
url: "data.php?order=<?php echo isset($_GET['order'])?!$_GET['order']:1; ?>",
type: 'POST',
asynch: false,
data: {
"sort_by_name":1
},
success: function(d){
$('#movie_container').html(d);
}
});
});
// PHP
if (isset($_POST['sort_by_name'])) {
$isAsc = isset($_GET['order']) ? (bool) $_GET['order'] : 1;
if ($isAsc) {
$sql = "SELECT id, movie_name, movie_year, movie_rating, movie_description, movie_imdb, movie_image, movie_my_rating FROM movies ORDER BY movie_name ASC";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$rowcount = mysqli_num_rows($result);
include 'show_movie_and_ribbon.php';
}
}
} else {
$sql = "SELECT id, movie_name, movie_year, movie_rating, movie_description, movie_imdb, movie_image, movie_my_rating FROM movies ORDER BY movie_name DESC";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$rowcount = mysqli_num_rows($result);
include 'show_movie_and_ribbon.php';
}
}
}
}
答案 0 :(得分:0)
使用JS获取url参数:
将订单参数插入数据。
检查此代码:
function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
}
$('#sort_by_name').click(function(e){
e.preventDefault();
$.ajax({
url: "data.php",
type: 'POST',
asynch: false,
data: {
"sort_by_name":1,
"order":getUrlParameter('order')?:1
},
success: function(d){
$('#movie_container').html(d);
}
});
});
if (isset($_POST['sort_by_name'])) {
$isAsc = isset($_POST['order']) ? $_POST['order'] : 1;
if ($isAsc) {
$sql = "SELECT id, movie_name, movie_year, movie_rating, movie_description, movie_imdb, movie_image, movie_my_rating FROM movies ORDER BY movie_name ASC";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$rowcount = mysqli_num_rows($result);
include 'show_movie_and_ribbon.php';
}
}
} else {
$sql = "SELECT id, movie_name, movie_year, movie_rating, movie_description, movie_imdb, movie_image, movie_my_rating FROM movies ORDER BY movie_name DESC";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$rowcount = mysqli_num_rows($result);
include 'show_movie_and_ribbon.php';
}
}
}