Я пытаюсь создать нумерацию страниц с ajax и php, но когда я нажимаю на ссылку нумерации страниц, не будет работать. Я попытался установить другой URL-адрес и изменил метод с метода get на post, но у меня все та же проблема. Вот библиотека php, которую я использовал вместе с методами моей модели и контроллера.
/ / Php Библиотека пагинации
<?php
/*
** PHP Pagination Class
*** https://github.com/dcblogdev/pagination
*/
class Paginator{
private $_perPage, $_instance, $_page, $_limit, $_totalRows = 0, $_customCSS;
public function __construct($perPage, $instance, $customCSS = ''){
$this->_instance = $instance;
$this->_perPage = $perPage;
$this->set_instance();
$this->_customCSS = $customCSS;
}
public function get_start(){
return ($this->_page * $this->_perPage) - $this->_perPage;
}
private function set_instance(){
$this->_page = (int) (!isset($_GET[$this->_instance]) ? 1 : $_GET[$this->_instance]);
$this->_page = ($this->_page == 0 ? 1 : $this->_page < 0 ? 1 : $this->_page);
}
public function set_total($_totalRows){
$this->_totalRows = $_totalRows;
}
public function get_limit(){
return "LIMIT ".$this->get_start().",$this->_perPage";
}
public function get_limit_keys(){
return ['offset' => $this->get_start(), 'limit' => $this->_perPage];
}
public function page_links($path='?',$ext = null){
$adjacents = "2";
$prev = $this->_page - 1;
$next = $this->_page + 1;
$lastpage = ceil($this->_totalRows/$this->_perPage);
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<ul class='pagination ".$this->_customCSS."'>";
if ($this->_page > 1){
$pagination.= "<li><a href='".$path."$this->_instance=$prev"."$ext' id='click-pagination' data-id='".$counter."'>Prev</a></li>";
}
if ($lastpage < 7 + ($adjacents * 2)){
$pagination.= "<li><a href='".$path."$this->_instance=$counter"."$ext' id='click-pagination' data-id='".$counter."'>$counter</a></li>";
}
}
else{
if ($this->_page < $counter - 1){
$pagination.= "<li><a href='".$path."$this->_instance=$next"."$ext' id='click-pagination' data-id='".$counter."'>Next</a></li>";
}
else{
$pagination.= "</ul>\n";
}
}
return $pagination;
}
}
// мои методы контроллера и контроллера
//Controller method
public function showAllEvents($page = false)
{
if (isset($_GET['page'])) { $page = $_GET['page']; }
$pages = new Paginator('10', $page);
$countallevent = $pages->set_total( $this->model->countAllEvents() );
$allevent = $this->model->showAllEvents( $pages->get_limit() );
$pagelink = $pages->page_links('/event/?page');
$html ='';
foreach ($allevent as $r) {
$html .= '<div>' .ucfirst($r->event_name). '</div>';
}
$html .= $pagelink;
echo json_encode($html);
}
//my model methods
public function showAllEvents($limit){
$stmt = $this->db->prepare(" SELECT * events" . $limit );
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
public function countAllEventss(){
$stmt = $this->db->prepare("SELECT * Events");
$stmt->execute();
return $stmt->rowCount();
}
// мой индекс. php
<div class="showAllEvents"></div>
<script>
(function($){
showAllEvent();
$(document).on('click', '#click-pagination', function(e){
e.preventDefault();
var page = $(this).data('id');
showAllEvents(page);
})
})(jQuery);
function showAllEvents(page){
$.ajax({
url: '<?= URL; ?>' + '/Pastor/showAllEvents',
method: 'post',
data:{ page : page},
dataType : 'JSON',
success: function(data){
$('.showAllEvent').html(data);
}
});
}