R
R
rusgayfer2018-03-09 14:41:51
PHP
rusgayfer, 2018-03-09 14:41:51

How to display page switches correctly?

I display page switches like this:

// количество записей, выводимых на странице
$per_page=25;
// получаем номер страницы
if (isset($_GET['page'])) $page=($_GET['page']-1); else $page=0;
// вычисляем первый оператор для LIMIT
$start=abs($page*1);
// дальше выводим ссылки на страницы:
$stmNew = $db->dbStream->prepare("SELECT COUNT(*) FROM `asks` ORDER BY `group_id`");
try {
    $stmNew->execute();
} catch (PDOException $error) {
    trigger_error("Ошибка при работе с базой данных: {$error}");
}
$allsgroupss = $stmNew->fetch(PDO::FETCH_ASSOC);
$allgroupss = implode(",", $allsgroupss);
$total_rows = $allgroupss;

$num_pages=ceil($total_rows/$per_page);

for($i=1; $i<=$num_pages; $i++) {
  if ($i-1 == $page) {
  } else {
$strnum .= '<ul class="pagination"><li class="paginate_button"><a href="?page='.$i.'" aria-controls="example2" data-dt-idx="2" tabindex="0">'.$i .'</a></li></ul>';
  
  }
}

The problem is that it displays all the switches at once, and I need 10 switches to make it like this:
Назад 1 2 3 4 5 6 7 8 9 10 Вперед
and here it immediately displays everything from 1 to 2428 on the button page
5aa27324cb383907734863.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Immortal_pony, 2018-03-09
@rusgayfer

$minPage = $page-5 > 0 ? $page-5 : 0;
$maxPage = $page+5 < $num_pages ? $page+5 : $num_pages;

$strnum = '<ul class="pagination">';

if ($minPage > 0) {
    $strnum .= '<li class="paginate_button"><a href="?page='.($minPage-1).'" aria-controls="example2" data-dt-idx="2" tabindex="0">Назад</a></li>';
} 

foreach (range($minPage, $maxPage) as $i) {
    $currentPageClass = $i==$page ? ' active' : '';
    $strnum .= '<li class="paginate_button'.$currentPageClass.'"><a href="?page='.$i.'" aria-controls="example2" data-dt-idx="2" tabindex="0">'.$i .'</a></li>';
}

if ($maxPage < $num_pages) {
    $strnum .= '<li class="paginate_button"><a href="?page='.($maxPage+1).'" aria-controls="example2" data-dt-idx="2" tabindex="0">Вперед</a></li>';
} 

$strnum .= '</ul>';

B
Barmunk, 2018-03-09
@Barmunk

Get the first 5 pages, check which page you are on and move the pointer, displaying the next segment of the array
. I found a simple class on github that implements this https://github.com/lotsofcode/php-array-pagination...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question