38808-vm/includes/pagination.php
2026-03-27 09:36:07 +00:00

100 lines
3.3 KiB
PHP

<?php
/**
* Calculate pagination parameters.
*
* @param int $page Current page number (1-based)
* @param int $total Total number of items
* @param int $perPage Items per page (default 10)
* @return array ['offset' => int, 'limit' => int, 'total_pages' => int, 'current_page' => int]
*/
function getPagination($page, $total, $perPage = 10) {
$page = max(1, (int)$page);
$perPage = max(1, (int)$perPage);
$totalPages = ceil($total / $perPage);
// Ensure page doesn't exceed total pages (unless total is 0)
if ($totalPages > 0 && $page > $totalPages) {
$page = $totalPages;
}
$offset = ($page - 1) * $perPage;
return [
'offset' => $offset,
'limit' => $perPage,
'total_pages' => $totalPages,
'current_page' => $page
];
}
/**
* Helper to build pagination link with existing query params
*/
function getPaginationLink($page, $queryParams = []) {
$params = array_merge($_GET, $queryParams);
$params['page'] = $page;
return '?' . http_build_query($params);
}
/**
* Render pagination links using Bootstrap 5.
*
* @param int $currentPage
* @param int $totalPages
* @return string HTML for pagination
*/
function renderPagination($currentPage, $totalPages) {
if ($totalPages <= 1) {
return '';
}
$html = '<nav aria-label="Page navigation" class="mt-4"><ul class="pagination justify-content-center">';
// Previous Button
if ($currentPage > 1) {
$html .= '<li class="page-item"><a class="page-link" href="' . getPaginationLink($currentPage - 1) . '">السابق</a></li>';
} else {
$html .= '<li class="page-item disabled"><span class="page-link">السابق</span></li>';
}
// Page Numbers
// Simple logic: Show all if <= 7, else show start, end, and around current
// For simplicity in this iteration, let's show a sliding window or just simple list
// Let's do a simple sliding window of 5
$start = max(1, $currentPage - 2);
$end = min($totalPages, $currentPage + 2);
if ($start > 1) {
$html .= '<li class="page-item"><a class="page-link" href="' . getPaginationLink(1) . '">1</a></li>';
if ($start > 2) {
$html .= '<li class="page-item disabled"><span class="page-link">...</span></li>';
}
}
for ($i = $start; $i <= $end; $i++) {
$active = ($i == $currentPage) ? 'active' : '';
if ($i == $currentPage) {
$html .= '<li class="page-item active"><span class="page-link">' . $i . '</span></li>';
} else {
$html .= '<li class="page-item"><a class="page-link" href="' . getPaginationLink($i) . '">' . $i . '</a></li>';
}
}
if ($end < $totalPages) {
if ($end < $totalPages - 1) {
$html .= '<li class="page-item disabled"><span class="page-link">...</span></li>';
}
$html .= '<li class="page-item"><a class="page-link" href="' . getPaginationLink($totalPages) . '">' . $totalPages . '</a></li>';
}
// Next Button
if ($currentPage < $totalPages) {
$html .= '<li class="page-item"><a class="page-link" href="' . getPaginationLink($currentPage + 1) . '">التالي</a></li>';
} else {
$html .= '<li class="page-item disabled"><span class="page-link">التالي</span></li>';
}
$html .= '</ul></nav>';
return $html;
}