【问题标题】:How to avoid duplicate record on pagination in php?如何避免php中的分页重复记录?
【发布时间】: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


【解决方案1】:

您的searches == true 条件存在问题。那是每次在 HTML 中附加数据时。修理它。它会正常工作。简单移除条件 并使用以下代码:

if(data !== 'undefined' && data !== '') { 
 $("#rows").html(data); 
}

【讨论】:

  • 最后一次滚动后,所有数据都神奇地消失了。我检查它是因为它执行查询结果为 NO 数据。但是查询不应该在滚动时执行。
  • 它仍然会在最后一次滚动时从屏幕上消失。 @Gulshan
  • 上次滚动时你能把console.log(data)放在这里吗?
  • 这里是console.log(data) &lt;tr style="display:none;"&gt; &lt;td colspan="5"&gt; &lt;input type="hidden" name="total_page" id="total_page" value="2"&gt; &lt;input type="hidden" name="current_page" class="current_page" id="current_page" value="3"&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt;&lt;td colspan="12"&gt;No records found&lt;/td&gt;&lt;/tr&gt;
  • 在服务器端设置条件,如果result 为空,则不要return 任何内容。
【解决方案2】:

如果显示重复数据,则可以使用 DISTINCT 关键字从查询中修复

select DISTINCT from sakhe WHERE status = 1 ORDER BY sakhe_id ASC limit 0,30

和:

select DISTINCT from sakhe WHERE status = 1 ORDER BY sakhe_id ASC limit 30,30

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-18
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    相关资源
    最近更新 更多