Black Friday sale is now on! 50% off Laravel: The Modular Way. Learn more

Creating a blog from scratch with PHP - Part 6 Pagination

David Carr

Demos Blog PHP & MySQL Tutorials

  • Archives.php
  • catpost.php
  • Blog Series


    This tutorial is extending the Creating a blog from scratch with PHP with that in mind I will only be covering new pieces of code and not the whole codebase.

    This part will cover adding a pagination system to limit how many posts are displayed per page.

    Demo

    Download source files

    admin demo: http://www.dcblog.dev/demos/simpleblog-pagination/admin

    username: demo
    password: demo

    To get started download my paginator class from https://github.com/daveismyname/pagination save the file inside the classes folder and rename the class to class.paginator.php that way the autoloader will include the file when needed without having to include it manually.

    Now that the class is ready it will be used on index.php, archives.php and catpost.php, first open index,php

    Above the query on line 30 add:

    //instantiate the class
    $pages = new Paginator('5','p');
    
    //collect all records fro the next function
    $stmt = $db->query('SELECT postID FROM blog_posts_seo');
    
    //determine the total number of records
    $pages->set_total($stmt->rowCount());

    Paginator() expects 2 parameters the first is then number of records to display next is the character to use in the url the default is to use p so urls become ?p=1

    Now the class knows how many records to show and how many records their are in total $pages->get_limit() is ready to be used this will limit the query to the number of records to display.

    Update the query:

    $stmt = $db->query('SELECT postID, postTitle, postSlug, postDesc, postDate FROM blog_posts_seo ORDER BY postID DESC '.$pages->get_limit());

    The last step is to generate the links of pages , place the following after the while statement.

    echo $pages->page_links();

    Save the page, index.php will now show 5 posts per page, if their is less then 5 posts you won't see the page links otherwise a series of links will be shown, this will require some styling open style/main.css and add:

    /* PAGINATION */
    
    .pagination {
        clear: both;
        padding-bottom: 10px;
        padding-top: 10px;
    }
    .pagination a {
        border: 1px solid #D5D5D5;
        color: #666666;
        font-size: 11px;
        font-weight: bold;
        height: 25px;
        padding: 4px 8px;
        text-decoration: none;
      margin:2px;
    }
    .pagination a:hover, .pagination a:active {
        background:#efefef;
    }
    .pagination span.current {
        background-color: #687282;
        border: 1px solid #D5D5D5;
        color: #ffffff;
        font-size: 11px;
        font-weight: bold;
        height: 25px;
        padding: 4px 8px;
        text-decoration: none;
            margin:2px;
    }
    .pagination span.disabled {
        border: 1px solid #EEEEEE;
        color: #DDDDDD;
        margin: 2px;
        padding: 2px 5px;
    }

    Archives.php

    For this page a query will need to be ran to determine the total number of pages for the current month and year that has been selected.

    $pages = new Paginator('5','p');
    
    $stmt = $db->prepare('SELECT postID FROM blog_posts_seo WHERE postDate >= :from AND postDate <= :to');
    $stmt->execute(array(
        ':from' => $from,
        ':to' => $to
        ));
    
    //pass number of records to
    $pages->set_total($stmt->rowCount());

    Next add the limit to the existing query, replace the full query with:

    $stmt = $db->prepare('SELECT postID, postTitle, postSlug, postDesc, postDate FROM blog_posts_seo WHERE postDate >= :from AND postDate <= :to ORDER BY postID DESC '.$pages->get_limit());
    $stmt->execute(array(
    ':from' => $from,
    ':to' => $to
    ));

    For the list of links pass in a- followed by the selected month and year so they are retained when going to different page numbers, for instance page 2 the url will be a-5-2013&p=2

    echo $pages->page_links("a-$month-$year&");

    catpost.php

    This is the same again, the initial query will need to collect the total number of posts for the selected category only.

    $pages = new Paginator('1','p');
    
    $stmt = $db->prepare('SELECT blog_posts_seo.postID FROM blog_posts_seo, blog_post_cats WHERE blog_posts_seo.postID = blog_post_cats.postID AND blog_post_cats.catID = :catID');
    $stmt->execute(array(':catID' => $row['catID']));
    
    //pass number of records to
    $pages->set_total($stmt->rowCount());

    The query for the posts will need to include the limit.

    $stmt = $db->prepare('
    SELECT 
        blog_posts_seo.postID, blog_posts_seo.postTitle, blog_posts_seo.postSlug, blog_posts_seo.postDesc, blog_posts_seo.postDate 
    FROM 
        blog_posts_seo,
        blog_post_cats
    WHERE
         blog_posts_seo.postID = blog_post_cats.postID
         AND blog_post_cats.catID = :catID
    ORDER BY 
        postID DESC
    '.$pages->get_limit());

    The pages links will need to include the url patten including the currently selected category.

    echo $pages->page_links('c-'.$_GET['id'].'&');

    Now all the blog pages will be limited to how many posts are displayed per page.

    Laravel Modules Book by David Carr

    Help support the blog so that I can continue creating new content!

    Subscribe to my newsletter

    Subscribe and get my books and product announcements.

    © 2009 - 2022 DC Blog. All code MIT license. All rights reserved.