Web Pagination Using PHP and MySQL

What is web pagination? Previously, we'll know a little about web pagination. Web pagination is a method of presenting data on web pages by dividing it into several pages and make the numbering on the pages where data is displayed quite a lot. Web pagination is often found on the homepage or on the page that displays the search results. The easiest example is found on a Google search results page.
On this occasion I will share to make a simple web pagination using php and MySQL. We need some function to create a web pagination, the following functions are:

// connection function used to connect database
function connection($db,$host,$username,$pwd){
      return true;
      return false;
    return false;

// getfirst function is used to determine the initial position of record.
function getfirst($activepage,$limit){
  if((empty($activepage) || ($activepage==0))){
      $first=($activepage-1)* $limit;
  return $first;

// getpage function to get the number of pages
function getpage($recordcount,$limit){
  return ceil($recordcount/$limit);

// createnavigator function to create page navigator
function createnavigator($url,$activepage,$allpage){
      $link .="First | Prev | ";
      $link .="<a href=\"$tmp\">First</a> | ";
      $link .="<a href=\"$tmp\">Prev</a> | ";
    for($i=($activepage-5);$i<=($activepage+5); $i++){
     if(($i>0 && $i<=$allpage)){
        if ($i == $activepage){
           $link .= "$i |";
           $link .="<a href=\"$tmp\">$i</a> | ";
        $link .= " ";
      $link .="Next | Last | ";
      $link .="<a href=\"$tmp\">Next</a> | ";
      $link .="<a href=\"$tmp\">Last</a>";
    return $link;

Once these functions are made, the next step is to connect to the database by calling the function connection like this:
"web_pagination" is a database, "localhost" is a local server or your servername, "root" is the username and "" is the password, the password blank on my local server.

Then typing this script to determine the current page and limit the data displayed on each page like this:
$activepage=1;// define active page
$limit=5;// limit the number of records that appear on every page, change the value of the variable to change the limit
Call getfirst function to get the starting position of the record to be displayed:
Use query to get records that will be displayed and the total number of records:
// using query to get book_data with limit
$q1=mysql_query("SELECT * FROM book_data ORDER BY book_title LIMIT $first,$limit");
// using query to get record count of all record
$q2=mysql_query("SELECT count(book_id) AS recordcount FROM book_data ORDER BY book_title");
Using this script to display data:
     echo " <tr>";
     echo "  <td style=\"font-size:12px;color:#555;text-align:left;\">&nbsp;$data[book_title]</td><td style=\"font-size:12px;color:#555;text-align:left;\">&nbsp;$data[writer]</td><td style=\"font-size:12px;color:#555;text-align:left;\">&nbsp;$data[publisher]</td><td style=\"font-size:12px;color:#555;text-align:left;\">&nbsp;$data[isbn]</td><td style=\"font-size:12px;color:#555;text-align:left;\">&nbsp;$data[publication_date]</td>";
     echo " </tr>";
Call getpage function to get the total number of pages and createnavigator function to display page navigator on the bottom like this:
echo createnavigator("index.php?page=",$activepage,$allpage);
Download: source code