【发布时间】:2020-01-23 08:50:03
【问题描述】:
我已经在 php 中进行了分页。但在某些记录之后,它会显示某些行的重复条目。
我的查询是第一次加载页面时
select * from sakhe WHERE status = 1 ORDER BY sakhe_id ASC limit 0,30
我在第二次滚动时的查询(一次滚动时执行了两次)
select * from sakhe WHERE status = 1 ORDER BY sakhe_id ASC limit 30,30
在这个查询之后,我在控制台中找到了正确的数据。但它在现场显示重复。
我的js代码是这样的
var page = 1;
var total_page = "";
function getresult(searches) {
$.ajax({
url: "process/Sakhe_list.php",
type: "post",
data: {
Sakhe_name: $("#Sakhe_name").val(),
page: page
},
success: function (data) {
if (searches == true) {
$("#rows").html(data);
} else {
$("#rows").append(data);
}
}
});
}
$(document).ready(function () {
getresult(false);
$(window).scroll(function () {
if ($(window).scrollTop() >= $(document).height() - $(window).height() - 5) {
if ($(".current_page:last").val() <= $("#total_page").val()) {
page = parseInt($(".current_page:last").val()) + 1;
getresult(false);
}
}
});
$("#shopsubmit").click(function () {
page = 1;
getresult(true);
});
$("#reset").click(function () {
$("#Sakhe_name").val("");
page = 1;
getresult(true);
});
我的PHP代码是这样的
extract($_REQUEST);
$perPage = 30; // total records per page
// page
$conditionArr = array(); // array for condition
$condition = ""; // conditions
$pages = ""; // how many pages created
$type = "";
$company = "";
$message = "";
$rersArr = array();
$data = array();
$templeArr = array();
if ($Sakhe_name != "") {
array_push($conditionArr, "name LIKE '%" . $Sakhe_name . "%'");
}
$sql = "select * from sakhe WHERE status = 1 ";
if (sizeOf($conditionArr) > 0) {
$condition = implode(" AND ", $conditionArr);
// echo $condition;
$sql .= " AND $condition";
}
$start = ($page - 1) * $perPage;
$sql1 = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$count = mysqli_num_rows($sql1);
$pages = ceil($count / $perPage);
$sql .= " ORDER BY sakhe_id ASC";
$sql .= " limit $start,$perPage ";
print_r("final ".$sql);
?>
<tr style="display:none;">
<td colspan="5">
<input type="hidden" name="total_page" id="total_page" value="<?php echo $pages; ?>">
<input type="hidden" name="current_page" class="current_page" id="current_page"
value="<?php echo $page; ?>">
</td>
</tr>
目前我正在获取最后 7 条重复记录。
【问题讨论】:
-
您使用的是哪个 dbms?
-
使用
DISTINCT关键字。 -
我正在使用 MySql @jarlh
-
HTML 中的
append数据存在问题。这种情况是否有效searches == true? -
@MeetPatel 每次数据都是 HTML 中的
append。修理它。它会正常工作。简单删除条件并使用$("#rows").html(data);
标签: javascript php mysql sql pagination