Paging Using PHP – Info PHP

Introduction

This article explains how to handle a number of records using paging. Whenever we think of paging we think of a fixed number of files being divided into a number of pages, something like in computer architecture. Computer architecture of paging comes under memory management, where the processor divides the fixed size of a page to map the physical memory or hard disk or any any other storage device. In this article we will see the concepts of paging or something like that.  

Use the following procedure for paging in PHP:

  • Create a table in SQL Server
  • Create a connection with SQL Server in your PHP application
  • Display the table data
  • Apply the paging logic
  • Output

Create a table in SQL Server

Creating a table in SQL Server is a very simple task. So in this project I created a table named “paging” and filled in some dummy data inside that table.

Create connection with SQL Server in your PHP application

This is the next phase, in this phase we need to write PHP code and make a connection with the SQL Server or any other database server. For making a connection with SQL Server using PHP, we should use Microsoft Drivers API and for the connection the sqlsrv_connect() function is used and in this function we should pass the server name and database name with the credentials (user name and password).

  1.   
  2.   
  3. <a href="https://www.onlyinfotech.com/tag/paging/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Paging">Paging</a> in PHP  
  4.   
  5.   
  6. $serverName = “local”;  
  7. $connectionInfo = array(“UID”=>“sa”,“PWD”=>“urPwd”,“Database”=>“Practice”);  
  8. $con = sqlsrv_connect( $serverName$connectionInfo);  
  9.  if$con === false )  
  10.  {  
  11.      echo “Could not connect.n”;  
  12.      die( print_r( sqlsrv_errors(), true ));  
  13.  }  
  14.   
  15.   

Display the table data

After connecting with SQL Server, now we display all the table data in PHP. For displaying the table data use the following code snippet. 

  1.   
  2.   
  3. Paging in PHP  
  4.   
  5.   
  6. $serverName = “local”;  
  7. $connectionInfo = array(“UID”=>“sa”,“PWD”=>“urPWD”,“Database”=>“Practice”);  
  8. $con = sqlsrv_connect( $serverName$connectionInfo);  
  9.  if$con === false )  
  10.  {  
  11.      echo “Could not connect.n”;  
  12.      die( print_r( sqlsrv_errors(), true ));  
  13.  }  
  14. $str= sqlsrv_query($con“SELECT * FROM Paging”);  
  15.   while($row= sqlsrv_fetch_array($str))  
  16.    {  
  17.         echo $row[“ID”]. ”    “ . $row[“Name”];  
  18.         echo 
    ;  
  19.    }  
  20. ?>  
  21.   
  22.   

pagingRowData

Apply the paging logic

Step 1 For the paging logic, first of all we should pass a query that displays only five (5) records at a time, for that we should write a query like:
 “SELECT * FROM Paging ORDER BY ID OFFSET 0 ROWS FETCH NEXT 05 ROWS ONLY”
But in the case of MySQL, you should use:
 “SELECT * FROM Paging LIMIT 0,5”

Now count the number of records and divide those records into 5, the code snippet is below.

  1. $str1= sqlsrv_query($con“SELECT * FROM Paging”,array(), array“Scrollable” => SQLSRV_CURSOR_KEYSET ));  
  2. $count= sqlsrv_num_rows($str1);  
  3. $a$count/5;  
  4. $a=ceil($a);  
  5. echo 
     number of recoreds – “
     .$count;   

logic1

Step 2 Now in the next phase we should make a hyperlink and bind all the data using a query string.
We get the data from the query string as a page variable. Paging.?page=
This code tells us to “page”, it is the first query string value and $i assigns some values in the page variable.

  1. for($i=1;$i<=$a$i++)  
  2. {  
  3.     ?> “Paging.php?page= style=” text-decoration:none; “ >echo $i”     “ ?>
  4.  }  

Step 3 From now on we will be getting the value and passing that value into the SQL query for proper output.
So, for getting the value of the query string , we use $_GET. $_GET is a supergloabal or automatic gloabal variable. This simply means that it is available in all scopes throughout the script. The following code explains how to consume the query string value.

  1. if(!isset($_GET[“page”]))  
  2. {  
  3.     $page=“1”;   
  4. }  
  5. else{  
  6.     $page=$_GET[“page”];  
  7. }  
  8.    
  9. if($page==“” || $page==“1”)  
  10. {  
  11.     $page1=0;  
  12. }  
  13. else{  
  14.     $page1= (($page*5)-5);  
  15. }  

Step 4 The next and final stage of implementation of the logic. In this section we use very simple logic and the logic is (($page*5)-5)
If we look at this code carefully in every cycle it will return a multiple of 5 as output. Like if the value of $page is 0 then the output is 0 and when the value of $page is 1 then the output will be 5, the next time $page is 2 , output will be 10 and so on. This output value we passed into the $page1 (another variable). And this $page1 variable is passed into the query .   

 

  1. $serverName = “local”;  
  2. $connectionInfo = array(“UID”=>“sa”,“PWD”=>“urPWD”,“Database”=>“Practice”);  
  3. $con = sqlsrv_connect( $serverName$connectionInfo);  
  4.   if$con === false )  
  5.   {  
  6.       echo “Could not connect.n”;  
  7.       die( print_r( sqlsrv_errors(), true ));  
  8.   }  
  9.   if(!isset($_GET[“page”]))  
  10.   {  
  11.       $page=“1”;  
  12.   }  
  13.   else{  
  14.       $page=$_GET[“page”];  
  15.   }  
  16.   
  17.  if($page==“” || $page==“1”)  
  18.   {  
  19.       $page1=0;  
  20.   }  
  21.   else{  
  22.       $page1= (($page*5)-5);  
  23.   }  
  24. $str= sqlsrv_query($con“SELECT * FROM Paging ORDER BY ID  OFFSET  $page1 ROWS  FETCH NEXT 05 ROWS ONLY”);  
  25.   while($row= sqlsrv_fetch_array($str))  
  26.   {  
  27.       echo $row[“ID”]. ”    “ . $row[“Name”];  
  28.       echo 
    ;  
  29.   }  
  30.   
  31. $str1= sqlsrv_query($con“SELECT * FROM Paging”,array(), array“Scrollable” => SQLSRV_CURSOR_KEYSET ));  
  32. $count= sqlsrv_num_rows($str1);  
  33. $a$count/5;  
  34. $a=ceil($a);  
  35. echo 
     ;  
  36. for($i=1;$i<=$a$i++)  
  37. {  
  38.    ?> “Paging.php?page= style=” text-decoration:none; “ >echo $i”     “ ?>
  39. }  
  40. ?>  

Output

output1

Summary
 
In this article we learned how to use paging in PHP.

Article Prepared by Ollala Corp

You might also like More from author

Leave A Reply

Your email address will not be published.