Friday, 18 September 2020

A basic pagination function for a blog system

PHP 

On a blog's front page, the pagination is a common way to navigate users from on page to another to browse the list of posts. According to the page number, we need to get the relevant posts' meta data, e.g. titles, descriptions, dates, authors and tags.

We can use an array to store each post's meta data, for example:

$post = array(
            'Date' => '2020-09-01',
            'Title' => 'Java programming tutorial 1',
            'Description' => 'Introduction to Java programming',
            'Author' => 'David',
            'Tag' => 'Java, Programming, Tutorial'
        );

Suppose all posts' meta data is retrieved and stored in the array $posts, we can then get the relevant posts' meta data based on the input page number.

const PAGE_LIMIT = 10;

function getPagination($posts, $page_number) {

    $total_pages = ceil(count($posts) / PAGE_LIMIT);
    $offset = ($page_number - 1) * PAGE_LIMIT;
    $paged_posts = array_slice($posts, $offset, PAGE_LIMIT);    

    $show_pages = array('Total pages' => $total_pages,
                        'Paged posts' => $paged_posts    
                  );

    return $show_pages;

}

In the above code snippet, we define the constant PAGE_LIMIT for the number of posts displayed on each page. In this example, we allow 10 posts to be displayed on each page. The function getPagination($posts, $page_number) gets the relevant posts' meta data based on the input page number $page_number. In line 5, we calculate the total number of pages for all posts. Then, we calculate the offset, i.e. the start point, based on the input page number $page_number. The PHP function array_slice() get the sequence of elements from the input array $posts according to the offset $offset and the length PAGE_LIMIT. Finally, we return an array with the total number of pages and the posts that need to be displayed on the page $page_number.

To try the function getPagination($posts, $page_number), we can first generate the sample data.


$template = array(
            'Date' => '2020-09-01',
            'Title' => 'Java programming tutorial',
            'Description' => 'Introduction to Java programming',
            'Author' => 'David',
            'Tag' => 'Java, Programming, Tutorial'
        );

$posts = array();

for ($i = 1; $i < 37; $i++) {
    $post = $template;
    $post['Title'] = $post['Title'].' '.$i;
    array_push($posts, $post);
} 

In the above code snippet, we create a template for a post's meta data $template. In the for loop, we generate total 36 posts' meta data and store it in the array $posts.

Suppose we want to get the 4th page of the posts, we can do as below:

$page_number = 4;
$show_pages = getPagination($posts, $page_number);

echo 'Total pages: ';
echo $show_pages['Total pages'].'<br>';
echo 'Current page: '.$page_number.'<br>';
print_r($show_pages['Paged posts'][0]['Title']);

It will output:

Total pages: 4
Current page: 4
Java programming tutorial 31


Search