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){
        $totalPage=ceil($totalRec/$maxRec);
        $str="";
        
        /*prev button*/
        if($curPage>1){             
            $prevPage = $curPage-1;
            $str.=" ".makeLink("prev","?p=".$prevPage)." ";            
        }
        
        /*generate page number*/
        for($i=1;$i<=$totalPage;$i++){
            if($i==$curPage){
                $bold=true;
            }else{
                $bold=false;
            }
            $str.=" ".makeLink($i,"?p=".$i,$bold)." ";
        }
        
        /*next button*/
        if($curPage<$totalPage){             
            $nextPage=$curPage+1;
            $str.=" ".makeLink("next","?p=".$nextPage)." ";            
        }
        
        return $str;
        
    }
    function makeLink($str,$url,$bold="false"){
        if($bold){
            $str="<b>".$str."</b>";
        }
        return '<a href="'.$url.'">'.$str.'</a>';
    }
    
        
    //Connect to database    
    $con = mysql_connect("localhost","root","");
    mysql_select_db("example",$con);
    
    $curPage=($_GET['p']==null)?1:$_GET['p']; 
    $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>";
    $rs=mysql_query($sql,$con);
    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);    
    $total_record=mysql_fetch_row($total_record);
    $total_record=$total_record[0];
        
    
    /* PRINT PAGING LINK */
    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.

 

Multiple Delete (PHP)

Now I will try to share a little about how to create a facility to delete a record on the website (php) similar to delete messages in GMail.

  1. First, create a form and then show a list of records retrieved from the database. View in a table where each record by 1 checkbox. 
    <?
    
           $rs=mysql_query("select * from mhs");//ambil record dari tabel mhs
    
           while($data=mysql_fetch_array($rs)){?>
    
              <tr>
    
                 <td>
    
                  <input type="checkbox" name="chk[]" value="<?=$data[id]?>">
    
                 </td>
    
                  <td><?=$data[id]?></td>
    
                  <td><?=$data[nim]?></td>
    
                  <td><?=$data[nama]?></td>
    
               </tr><?
    
           }
    
    ?>

    Checkbox on top of each filled with the id of each record and the name of the checkbox by the same name and added symbol behind the name of “[]”. This is so that when read by PHP, Checkbox considered as an array.

  2. <?
    
              $chk=$_POST[chk];
    
              / * -------------- Change corresponding computer settings masing2 ---- * /
    
              $host="localhost";
    
              $user="root";
    
              $pass="";
    
             /*--------------------------------------------------------*/
    
              $ Con = mysql_connect ($ host, $ user, $ pass); // open connection to the database
    
              mysql_select_db("tes");//pilih database
    
     
              foreach($chk as $id){
    
                  if(mysql_query("delete from mhs where id=".$id)){
    
                      echo "<div align=center>
    
                              Remove record with id = ". $ Id." - Success
    
                            </div><br>";
    
                  }else{
    
                      echo "<div align=center>
    
                              Delete record with id=".$id."-- Failed
    
                            </div><br>";
    
                  }     
    
              }
    
        mysql_close($con);
    
    ?>

     After that create a new php file to delete process.

    First, do any data retrieval checkbox marked. As already known, to take the value sent from a form can use the $ _POST , $ _GET , or $ _REQUEST . Well, to take a checkbox value is the same way, here I am using the POST method.

           $chk=$_POST[chk];

     Because chk on html form written like this ” chk [] ” then the current value of the $ _POST pemgambilan then php will directly menggangap chk it as an array. Then the variable $ chk now be an array that contains the value of checkbox- checkbox is selected / checked.

    When he could take valuenya, then stay manipulate value-value is taken as necessary (in this example we will use valuenya as key to delete the record from the database. It can be seen from the above source code, I use foreach to take one-on-one contents array (array content is id which records obtained from the checkbox is checked only). Later in the recurrence did query delete :

    foreach($chk as $id){
    
        if(mysql_query("delete from mhs where id=".$id)){
    
            echo "<div align=center>
    
                     Remove record with id = ". $ Id." - Success
    
                 </div><br>";
    
        }else{
    
            echo "<div align=center>
    
                     Delete record with id=".$id."-- Failed
    
                  </div><br>";
    
        }     
    
    }