Showing posts with label OFFSET. Show all posts
Showing posts with label OFFSET. Show all posts

Wednesday, July 8, 2015

PHP basic pagination with MySQL

First step: create new branch in GitHub from the GitHub. Code. Download the code, from the GitHub application on local machines. Modify the code, commit changes to this branch and synchronize with GitHub server.  When I finished with the development I make a Pull Request to merge the branch with the repository.

I will display a certain number of posts per page (now is hard coded to 2), and 2 buttons (links) "Older posts" and "Newer Posts".

I am using MySQL "LIMIT" clause to specify the number of records to be returned. From http://www.w3schools.com/php/php_mysql_select_limit.asp
     
     The SQL query below says "return only 10 records, start on record 16 (OFFSET 15)":
        $sql = "SELECT * FROM Orders LIMIT 10 OFFSET 15";

It seems that the default type for bindParam is string so you will get an error if you do not specify to be an integer:   PDO:PARAM_INT:
    http://stackoverflow.com/questions/4544051/sqlstate42000-syntax-error-or-access-violation-1064-you-have-an-error-in-you
       
        $sth=$this->db->prepare("SELECT * FROM blogposts LIMIT :per_page OFFSET :page_number ;");
        $sth->bindParam(':per_page', $per_page, PDO::PARAM_INT);
        $sth->bindParam(':page_number', $page_numb, PDO::PARAM_INT);
        $sth->execute();



-added pagination.xml file in \model to store current page number and number of posts per page (this should be changed in the future as having one current page for all the users accessing the website is not right.)  Later edit: problem solved in this post http://phpbeginnertoadvanced.blogspot.co.uk/2015/07/fix-pagination.html

-added routes for /blog/older and /blog/newer 

You will have access to the code files when releasing the v0.4