PHP:使用Ajax进行分页无法正常工作

时间:2017-08-01 11:18:09

标签: php ajax

我从数据库中获取记录并使用php和ajax在gruops中显示它们。

下面的脚本(在某处找到)几乎正常工作,但是当您在第一页并点击“下一步”时,您将进入最后一页。

仅在单独单击数字时,分页才正确。

如果需要,我可以在PM中提供测试链接。

的index.php:

<script>
$(document).ready(function() {
    $("#results" ).load( "pages.php"); //load initial records
    //executes code below when user click on pagination links
    $("#results").on( "click", ".pagination a", function (e){
        e.preventDefault();
        var page = $(this).attr("data-page"); //get page number from link
        $("#results").load("pages.php",{"page":page}, function(){ //get content from PHP page
        });
    });
});
</script>
<div id="results"></div>

pages.php:

<?php
// continue only if $_POST is set and it is a Ajax request
if (isset($_POST) && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    include (realpath(__DIR__ . '/../db.php'));
    // Get page number from Ajax POST
    if (isset($_POST["page"])) {
        $page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number
        if (!is_numeric($page_number)) {
            die('Invalid page number!');
        }
    }
    else {
        $page_number = 1; //if there's no page number, set it to 1
    }
    // get total number of records from database for pagination
    $sql = "SELECT * FROM " . $DBtable . " ORDER BY dates DESC";
    $rs = $conn->query($sql);
    if ($rs === false) {
        trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
    }
    else {
        $get_total_rows = $rs->fetch_row(); //hold total records in variable
    }
    // break records into pages
    $total_pages = ceil($get_total_rows[0] / $item_per_page);
    // get starting position to fetch the records
    $page_position = (($page_number - 1) * $item_per_page);
    // Limit our results within a specified range.
    $rs = $conn->prepare("SELECT id, title, description, dates, clicks FROM " . $DBtable . " ORDER BY dates DESC LIMIT $page_position, $item_per_page");
    $rs->execute(); //Execute prepared Query
    $rs->bind_result($id, $title, $description, $dates, $clicks); //bind variables to prepared statement

    echo '<ul class="contents">';
    while($rs->fetch()){ //fetch values
        echo '<li>';
        echo  $id. '. <strong>' .$name.'</strong> &mdash; '.$description;
        echo '</li>';
    }
    echo '</ul>';
    echo '<div align="center">';
    /* We call the pagination function here to generate Pagination link for us. */
    echo paginate_function($item_per_page, $page_number, $get_total_rows[0], $total_pages);
    echo '</div>';
    exit;
}
// ############### pagination function #########################################
function paginate_function($item_per_page, $current_page, $total_records, $total_pages) {
    $pagination = '';
    if ($total_pages > 0 && $total_pages != 1 && $current_page <= $total_pages) { //verify total pages and current page number
        $pagination.= '<ul class="pagination">';
        $right_links = $current_page + 3;
        $previous = $current_page - 3; //previous link
        $next = $current_page + 1; //next link
        $first_link = true; //boolean var to decide our first link

        if ($current_page > 1) {
            $previous_link = ($previous == 0) ? 1 : $previous;
            $pagination.= '<li class="class="page-item" first"><a class="page-link" href="#" data-page="1" title="First">&laquo;</a></li>'; //first link
            $pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $previous_link . '" title="Prev">&lt;</a></li>'; //previous link
            for ($i = ($current_page - 2); $i < $current_page; $i++) { //Create left-hand side links
                if ($i > 0) {
                    $pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $i . '" title="Page' . $i . '">' . $i . '</a></li>';
                }
            }
            //set first link to false
            $first_link = false;
        }
        if ($first_link) { //if current active page is first link
            $pagination.= '<li class="page-item active first"><a class="page-link" href="#">' . $current_page . '<span class="sr-only">(current)</span></a></li>';
        }
        elseif ($current_page == $total_pages) { //if it's the last active link
            $pagination.= '<li class="page-item active last"><a class="page-link" href="#">' . $current_page . '<span class="sr-only">(current)</span></a></li>';
        }
        else { //regular current link
            $pagination.= '<li class="page-item active"><a class="page-link" href="#">' . $current_page . '<span class="sr-only">(current)</span></a></li>';
        }
        for ($i = $current_page + 1; $i < $right_links; $i++) { //create right-hand side links
            if ($i <= $total_pages) {
                $pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $i . '" title="Page ' . $i . '">' . $i . '</a></li>';
            }
        }
        if ($current_page < $total_pages) {
            $next_link = ($i > $total_pages) ? $total_pages : $i;
            $pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $next_link . '" title="Next">&gt;</a></li>'; //next link
            $pagination.= '<li class="page-item last"><a class="page-link" href="#" data-page="' . $total_pages . '" title="Last">&raquo;</a></li>'; //last link
        }
        $pagination.= '</ul>';
    }
    //return pagination links
    return $pagination;
}
// Free memory
$rs->free();
// Close connection
$conn->close();
?>

这是寻呼机当前解析的html:

<ul class="pagination">
  <li class="page-item active first"><a class="page-link" href="#">1<span class="sr-only">(current)</span></a></li>
  <li class="page-item"><a class="page-link" href="#" data-page="2" title="Page 2">2</a></li>
  <li class="page-item"><a class="page-link" href="#" data-page="3" title="Page 3">3</a></li>
  <li class="page-item"><a class="page-link" href="#" data-page="4" title="Next">&gt;</a></li>
  <li class="page-item last"><a class="page-link" href="#" data-page="4" title="Last">»</a></li>
</ul>

2 个答案:

答案 0 :(得分:0)

请在paginate_function函数中进行更改,如下所示

if ($current_page < $total_pages) {
        $next_link = ($i > $total_pages) ? $total_pages : $i;
        $pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $next_link . '" title="Next">&gt;</a></li>'; //next link
        $pagination.= '<li class="page-item last"><a class="page-link" href="#" data-page="' . $total_pages . '" title="Last">&raquo;</a></li>'; //last link
}

将此更改为

if ($current_page < $total_pages) {
        $next_link = ($i > $total_pages) ? $total_pages : $next;
        $pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $next_link . '" title="Next">&gt;</a></li>'; //next link
        $pagination.= '<li class="page-item last"><a class="page-link" href="#" data-page="' . $total_pages . '" title="Last">&raquo;</a></li>'; //last link
}

答案 1 :(得分:0)

只需在<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script> <div ng-app="scopePropertiesModule"> <div ng-controller="Ctrl3"> <input select-text ng-model="title"> </div> </div>

中尝试以下内容即可

$ next_link =($ i&gt; $ total_pages)? $ total_pages:$ i;

$ next_link =(($ current_page + 1)&gt; $ total_pages)? $ total_pages:$ current_page + 1;