<?php
/**
* total : 게시물의 총 갯수
* limit : 한 화면의 게시글 총 갯수
* page_limit : 출력페이지 수
* page : 현재페이지
*/
function my_pagination($total, $limit, $page_limit, $page) {
// 총 페이지 수
$total_page = ceil($total / $limit);
$start_page = ( ( floor( ($page - 1 ) / $page_limit ) ) * $page_limit ) + 1;
$end_page = $start_page + $page_limit -1;
// 현재 페이지 설정
$page = (isset($_GET['page']) && $_GET['page'] != '' && is_numeric($_GET['page'])) ? $_GET['page'] : 1;
// 시작 인덱스와 끝 인덱스 계산
$start = ($page - 1) * $limit;
$end = min(($start + $limit), $total);
// 끝 페이지가 총 페이지 수를 넘지 않도록 보정
if($end_page > $total_page) {
$end_page = $total_page;
}
$prev_page = $start_page - 1;
if($prev_page < 1) {
$prev_page = 1;
}
/*
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center">
<li class="page-item disabled"><a class="page-link">Previous</a>
</li>
<li class="page-item"><a class="page-link" href="#">1</a></li>
<li class="page-item"><a class="page-link" href="#">2</a></li>
<li class="page-item"><a class="page-link" href="#">3</a></li>
<li class="page-item">
<a class="page-link" href="#">Next</a>
</li>
</ul>
</nav>
*/
$rs_str = '<nav aria-label="Page navigation example">
<ul class="pagination">';
$rs_str .= '<li class="page-item"><a class="page-link" href="'.$_SERVER['PHP_SELF'].'?page=1">First</a></li>';
if($prev_page > 1) {
$rs_str .= '<li class="page-item"><a class="page-link" href="'.$_SERVER['PHP_SELF'].'?page='.$prev_page.'">Prev</a></li>';
}
for($i = $start_page; $i <= $end_page; $i++) {
if($i == $page) {
$rs_str .= "<li class=\"page-item active\">
<a class=\"page-link\" href=\"#\">{$i}</a>
</li>";
}else {
$rs_str .= '<li class="page-item"><a class="page-link" href="'.$_SERVER['PHP_SELF']."?page={$i}\">{$i}</a></li>";
}
}
$next_page = $end_page + 1;
if($next_page <= $total_page) {
$rs_str .= '<li class="page-item"><a class="page-link" href="'.$_SERVER['PHP_SELF']."?page={$next_page}\">Next</a></li>";
}
// last_page
if($page < $total_page) {
$rs_str .= '<li class="page-item"><a class="page-link" href="'.$_SERVER['PHP_SELF']."?page={$total_page}\">Last</a></li>";
}
$rs_str .= '</ul></nav>';
return $rs_str;
}