Simple Paging (PHP)

For programmers must often make applications that display data. Well, if the data is small, it does not matter, but what if the data that would show number in the dozens or even thousands of rows , will face a very long scroll window or may be your browser hang. Now, to overcome it used a system that is widely known that Paging System. Paging System is a system that displays the data step by step, it’s divided into several pages with each page has only a few data only.

Now I will explain a basic/simple logic of the paging system. See the table below:

For example, we had 22 records of student, then we want to show with divided into several pages with a maximum 5 records per page.
Well, first we find out the number of pages :

Number of Pages = Total Record / Maximum Record every page 
Number of Pages = 22/5 = 4.4 Pages round up to 5 Pages

For the data from the database (mysql), we take from the first record with a multiple of 5 to the next page, 
See table above:

  • on page 1, we just take the data from the record-0 up to record to 4,
  • on page 2, we simply take the data from record to record the 5th to the 9th, etc. (the first record in mysql starting from the number 0). 

So the formula to get the start record is:

(Current page number - 1) * Maximum records per page

So based on example above (with a maximum record per page = 5):
Start record for page 1: (1-1) * 5 = 0
Start record for page 2: (2-1) * 5 = 5
Start record for page 3 (3-1) * 5 = 10
Start record on page 4: (4-1) * 5 = 15
Start record to page 5: (5-1) * 5 = 20

Well, based on the logic that I described above , following the example in the form of coding PHP:

    function generate_paging_text($curPage,$totalRec,$maxRec){
        /*prev button*/
            $prevPage = $curPage-1;
            $str.=" ".makeLink("prev","?p=".$prevPage)." ";            
        /*generate page number*/
            $str.=" ".makeLink($i,"?p=".$i,$bold)." ";
        /*next button*/
            $str.=" ".makeLink("next","?p=".$nextPage)." ";            
        return $str;
    function makeLink($str,$url,$bold="false"){
        return '<a href="'.$url.'">'.$str.'</a>';
    //Connect to database    
    $con = mysql_connect("localhost","root","");
    $maxRec=5;// maximum record per page

    /* GET DATA FROM MYSQL */    
    $curRec = ($curPage-1)*$maxRec; //TO GET START RECORD
    $sql = "select * from customer order by name asc limit ".$curRec.",".$maxRec;
    echo "Query for Page  ".$curPage.": <b>".$sql."</b><Br><BR>";
    while ($row = mysql_fetch_assoc($rs)) {
        echo "Nama : ".$row['name']." , Umur : ".$row['age']."<br>";
    // get total Record from your table in database
    $total_record=mysql_query("select count(customer_id) as tot from customer",$con);    
    echo "<br>";
    echo generate_paging_text($curPage,$total_record,$maxRec);


function generate_paging_text($curPage,$totalRec,$maxRec)
  1. $curPage = current active page number.
  2. $totalRec = total records
  3. $maxRec = maximum records that may be displayed for each page.