Implementing Pagination in PHP with Custom Functions
This article explains the principle of pagination and provides three reusable PHP functions—get_total_pages, get_offset, and generate_pagination—along with example code to calculate total pages, determine record offsets, and render a navigation bar for efficient web page data paging.
Principle of Pagination
Pagination is typically based on database query results, dividing the result set into pages that each display a fixed number of records. Users navigate between pages by clicking page numbers or previous/next buttons, which changes the displayed subset of data.
In PHP, pagination can be achieved by calculating the total number of pages and the offset for the current page. The total pages are derived from the total record count divided by the number of records per page, while the offset is computed from the current page number and the page size.
Common Pagination Functions
get_total_pages()
This function calculates the total number of pages based on the total record count and the number of records per page.
function get_total_pages($total_records, $records_per_page) {
return ceil($total_records / $records_per_page);
}get_offset()
This function computes the offset for the current page, which determines where the database query should start retrieving records.
function get_offset($current_page, $records_per_page) {
return ($current_page - 1) * $records_per_page;
}generate_pagination()
This function builds an HTML pagination navigation bar based on the total number of pages and the current page. It creates links for previous, next, and individual page numbers, marking the active page.
function generate_pagination($total_pages, $current_page) {
$pagination = '';
if ($total_pages > 1) {
$pagination .= '<ul class="pagination">';
if ($current_page > 1) {
$pagination .= '<li><a href="?page=' . ($current_page - 1) . '">上一页</a></li>';
}
for ($i = 1; $i <= $total_pages; $i++) {
if ($i == $current_page) {
$pagination .= '<li class="active"><a href="?page=' . $i . '">' . $i . '</a></li>';
} else {
$pagination .= '<li><a href="?page=' . $i . '">' . $i . '</a></li>';
}
}
if ($current_page < $total_pages) {
$pagination .= '<li><a href="?page=' . ($current_page + 1) . '">下一页</a></li>';
}
$pagination .= '</ul>';
}
return $pagination;
}Implementing Pagination Using the Functions
To use the functions, first obtain the total record count, then call get_total_pages() to get the total pages. Next, call get_offset() to compute the offset for the current page, use it in the SQL LIMIT clause, and finally generate the navigation bar with generate_pagination().
// Get total record count
$total_records = 100;
// Records per page
$records_per_page = 10;
// Current page number
$current_page = isset($_GET['page']) ? $_GET['page'] : 1;
// Calculate total pages
$total_pages = get_total_pages($total_records, $records_per_page);
// Calculate offset for the current page
$offset = get_offset($current_page, $records_per_page);
// Build the query
$query = "SELECT * FROM table_name LIMIT $offset, $records_per_page";
// Execute the query ...
// Generate pagination navigation
$pagination = generate_pagination($total_pages, $current_page);
// Output results and pagination ...Conclusion
By using these PHP pagination functions, developers can easily implement efficient pagination, improving user experience. The article covered the underlying principle, provided reusable functions, and demonstrated a complete example for quick integration into web projects.
PHP Learning Recommendations
Vue3+Laravel8+Uniapp Beginner to Advanced Development Tutorial
Vue3+TP6+API Social E‑commerce System Development Course
Swoole From Beginner to Master Course
Workerman+TP6 Real‑time Chat System – Limited Time Offer
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
