ajax分页的每页动态记录

时间:2015-05-07 20:22:59

标签: jquery ajax

我正在尝试实现ajax分页。一切都没问题,但我每页的记录都有困难。在我的脚本中$recordsPerPage已修复,但我想让它动态化,以便用户可以选择记录数每页通过提供的选项。如何基于我的代码实现这一点?

$query="select * FROM ac WHERE status=1 order by id asc";
$res    = mysqli_query($connection,$query);
$count  = mysqli_num_rows($res);
$page = (int) (!isset($_REQUEST['pageId']) ? 1 :$_REQUEST['pageId']);
$page = ($page == 0 ? 1 : $page);
$recordsPerPage = 4;
$start = ($page-1) * $recordsPerPage;
$adjacents = "2";

$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($count/$recordsPerPage);
$lpm1 = $lastpage - 1;   
$pagination = "";
if($lastpage > 1)
    {   
        $pagination .= "<div class='pagination'>";
        if ($page > 1)
            $pagination.= "<a href=\"#Page=".($prev)."\" onClick='changePagination(".($prev).");'>&laquo; Previous&nbsp;&nbsp;</a>";
        else
            $pagination.= "<span class='disabled'>&laquo; Previous&nbsp;&nbsp;</span>";   
        if ($lastpage < 7 + ($adjacents * 2))
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class='current'>$counter</span>";
                else
                    $pagination.= "<a href=\"#Page=".($counter)."\" onClick='changePagination(".($counter).");'>$counter</a>";     

            }
        }
        elseif($lastpage > 5 + ($adjacents * 2))
        {
            if($page < 1 + ($adjacents * 2))
            {
                for($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if($counter == $page)
                        $pagination.= "<span class='current'>$counter</span>";
                    else
                        $pagination.= "<a href=\"#Page=".($counter)."\" onClick='changePagination(".($counter).");'>$counter</a>";     
                }
                $pagination.= "...";
                $pagination.= "<a href=\"#Page=".($lpm1)."\" onClick='changePagination(".($lpm1).");'>$lpm1</a>";
                $pagination.= "<a href=\"#Page=".($lastpage)."\" onClick='changePagination(".($lastpage).");'>$lastpage</a>";   

           }
           elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
           {
               $pagination.= "<a href=\"#Page=\"1\"\" onClick='changePagination(1);'>1</a>";
               $pagination.= "<a href=\"#Page=\"2\"\" onClick='changePagination(2);'>2</a>";
               $pagination.= "...";
               for($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
               {
                   if($counter == $page)
                       $pagination.= "<span class='current'>$counter</span>";
                   else
                       $pagination.= "<a href=\"#Page=".($counter)."\" onClick='changePagination(".($counter).");'>$counter</a>";     
               }
               $pagination.= "..";
               $pagination.= "<a href=\"#Page=".($lpm1)."\" onClick='changePagination(".($lpm1).");'>$lpm1</a>";
               $pagination.= "<a href=\"#Page=".($lastpage)."\" onClick='changePagination(".($lastpage).");'>$lastpage</a>";   
           }
           else
           {
               $pagination.= "<a href=\"#Page=\"1\"\" onClick='changePagination(1);'>1</a>";
               $pagination.= "<a href=\"#Page=\"2\"\" onClick='changePagination(2);'>2</a>";
               $pagination.= "..";
               for($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
               {
                   if($counter == $page)
                        $pagination.= "<span class='current'>$counter</span>";
                   else
                        $pagination.= "<a href=\"#Page=".($counter)."\" onClick='changePagination(".($counter).");'>$counter</a>";     
               }
           }
        }
        if($page < $counter - 1)
            $pagination.= "<a href=\"#Page=".($next)."\" onClick='changePagination(".($next).");'>Next &raquo;</a>";
        else
            $pagination.= "<span class='disabled'>Next &raquo;</span>";

        $pagination.= "</div>";       
    }

if(isset($_POST['pageId']) && !empty($_POST['pageId']))
{
    $id=$_POST['pageId'];
}
else
{
    $id='0';
}
$query="select * FROM ac WHERE status=1 order by id asc
limit ".mysqli_real_escape_string($connection,$start).",$recordsPerPage";

Ajax:

  <script type="text/javascript">
    $(document).ready(function(){
    changePagination('0');    
    });
    function changePagination(pageId){

         $(".flash").show();
         $(".flash").fadeIn(800).html
             ('Loading <img src="ajax-loader.gif" />');
         var dataString = 'pageId='+ pageId  ;
         $.ajax({
               type: "POST",
               url: "page.php",
               data: dataString,
               cache: false,
               success: function(result){
               $(".flash").hide();
               $("#pageData").html(result);
               }
          });
    }
    </script>

选项:

<select name="rp">
<option value="p1">5</option>
<option value="p2">10</option>            
<option value="p3">15</option>
<option value="p4">20</option>            
</select>

1 个答案:

答案 0 :(得分:0)

试试这个

<script type="text/javascript">
var currentPage = '0';
$(document).ready(function(){
changePagination();    
});
function changePagination(pageId){
     currentPage = !pageId ? currentPage : pageId;
     $(".flash").show();
     $(".flash").fadeIn(800).html
         ('Loading <img src="ajax-loader.gif" />');
     var dataString = {pageId: currentPage, recordsPerPage: $('select[name="rp"] option:selected').html()};
     $.ajax({
           type: "POST",
           url: "page.php",
           data: dataString,
           cache: false,
           success: function(result){
           $(".flash").hide();
           $("#pageData").html(result);
           }
      });
}
</script>

在您的php脚本中,读取recordsPerPage参数中发送的值:

$recordsPerPage = (int) (!isset($_REQUEST['recordsPerPage']) ? 4 :$_REQUEST['recordsPerPage']);