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){
  if(mysql_connect($host,$username,$pwd)){
    if(mysql_select_db($db)){
      return true;
    }
    else{
      return false;
    }
  }
  else{
    return false;
  }
}

// getfirst function is used to determine the initial position of record.
function getfirst($activepage,$limit){
  if((empty($activepage) || ($activepage==0))){
      $first=0;
  }
  else{
      $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){
    $tmp="";
    $link="";
    if($activepage==1){
      $link .="First | Prev | ";
    }
    else{
      $tmp=$url."1";
      $link .="<a href=\"$tmp\">First</a> | ";
      $prev=$activepage-1;
      $tmp=$url.$prev;
      $link .="<a href=\"$tmp\">Prev</a> | ";
    }
    for($i=($activepage-5);$i<=($activepage+5); $i++){
     if(($i>0 && $i<=$allpage)){
        if ($i == $activepage){
           $link .= "$i |";
        }
        else{
           $tmp=$url.$i;
           $link .="<a href=\"$tmp\">$i</a> | ";
        }
        $link .= " ";
      }
    }
    if($activepage==$allpage){
      $link .="Next | Last | ";
    }
    else{
      $next=$activepage+1;
      $tmp=$url.$next;
      $link .="<a href=\"$tmp\">Next</a> | ";
      $tmp=$url.$allpage;
      $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:
connection("web_pagination","localhost","root","");
"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
if(isset($_GET['page'])){
  if(preg_match("/[0-9]/",$_GET['page'])){
     $activepage=$_GET['page'];
  }
}
Call getfirst function to get the starting position of the record to be displayed:
$first=getfirst($activepage,$limit);
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");
$recordcount=mysql_fetch_array($q2);
Using this script to display data:
while($data=mysql_fetch_array($q1)){
     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:
$allpage=getpage($recordcount['recordcount'],$limit);
echo createnavigator("index.php?page=",$activepage,$allpage);
Download: source code