An easier way to do this is to use your model as much as possible and do it with sql "limit". I don’t know which database you are using, so I will take a few steps with the mysql database. I am embarrassed with your code, why do you call ajax without the press? or are you hiding some kind of code?
Let me try with this code: suppose you have a div container and 2 static navigation buttons
<a href="#" id="next" data-page="2">Next</a> <a href="#" id="back" data-page="0">Back</a> <div id="container"></div> <script> $("#next, #back").click(function(){ //get page number //0 means no page to load var page = $(this).data("page"); if(page == 0)return false; $.ajax({ url : "your/url/?page=" + page, dataType: "json", success : function(data){ var container = $("#container"); $.each(data, function(index, article){ container.append(article); // or do something you want }); if($this.attr("id") == "next") $("#next, #prev").data("page", $this.data("page") + 1); else $("#next, #prev").data("page", $this.data("page") - 1); } }); }); </script>
for backend:
<?php $page = $_GET["page"]; $perPage = 100; $start = ($page - 1) * $perPage; // minus 1, because limit start from 0 $sql = "SELECT * FROM table WHERE 1=1 LIMIT $start, $perPage"; //execute the sql and get the result (ex. result) echo json_encode($result);
I want my answer to help you.
source share