Answer the question
In order to leave comments, you need to log in
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>';
}
}
Назад 1 2 3 4 5 6 7 8 9 10 Вперед
Answer the question
In order to leave comments, you need to log in
$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>';
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question