Saturday, 5 August 2017

10 easy steps to create simple pagination using PHP

10 easy steps to create simple pagination using PHP


In this tutorial we will achieve pagination using PHP in 10 easy steps.  This is a simple pagination using PHP and mySQLi. Without using any plugin or Javascript and jQuery. PHP made pagination is very simple. In this tutorial we will follow 10 easy steps to create simple pagination using PHP for basic requirement of pagination. Like page links, Previous and Next button with auto hide and show as we need.

HTML Structure :

For just tutorial purpose we need only two Div Tags one for showing results and another for pagination control.

PHP Code :

For ease of understanding I have divided PHP code in steps :
  1. Database connection.
  2. Define how many records you want to display per page.
  3. Select data from Table.
  4. Determines total records in table.
  5. Calculate the total pages available.
  6. Observe on which page visitor is visiting.
  7. Setting for start and end record to display on page.
  8. Retrieve selected result on page.
  9. Close the database connection as we don’t require remain open any more.
  10. Making page links.
    • previous link appear when you are on page no. 2 or more than page no. 2 (optional)
    • page links
    • Next page link disappear when user visits on last page (optional).
Lets code to make simple pagination using PHP and mySQLi.

Step 1 : Database Connection

Here I am using Localhost and my database name is pagination.
  1. $conn = mysqli_connect("localhost", "root", "", "pagination") or die("Connection failed: " . mysqli_connect_error());

Step 2 : Define how many records you want to display per page.

I want to fetch 5 records per page. You can change it as per your requirement according to your data.
  1. $records_per_page = 5;

Step 3 : Select Data from table.

For demo purpose I have create a table name ‘data’ with two columns first is ‘ID’ and second is ‘Alphabets’ and inserted records.  Now we have to select data from table and run query.
  1. // selecting data from table named 'data'
  2. $sel = "SELECT * FROM data";
  3. // run the query and get all data in a local variable called $result
  4. $result = mysqli_query($conn, $sel);

Step 4 : Determines total records in table

Now we need to find how many records exist in our table by using mysqli_num_rows().
  1. $total_records = mysqli_num_rows($result);

Step 5 : Calculate the total pages available

Now we have to calculate the total pages available in our data table. Which we can easily  get by dividing total records to records per page. For ease of doing we have already done it by creating local variable $total_records and $record_per_page.
  1. $total_page = $total_records / $records_per_page;
As we know this result can be come in “.” (points) like 2.3 or 4.6. But our page cant display Like this. So we use here ceil() function make round off result. for example if we have 2.3 in total pages, the ceil() function will convert it in 3 or if we have 4.6 in total page the ceil() function convert it into 5.
So finally we use this code to get total page
  1. $total_page = ceil($total_records / $records_per_page);

Step 6 : Observe on which page visitor is visiting.

We can loop though the links for page control.
  1. for($page=1; $page<=$total_page;$page++){
  2. echo '<a href="pagination.php?page='.$page.'"> ' . $page . ' </a>';
  3. }
First of all we have to create a global variable using $_GET () method,  when user click on the particular link, this variable hold the page number value. So we can  easily achieve that which is page is currently visiting by user. By default page 1 is displayed. But user visited another page, we can check it by $_GET();
Note : We use the above code later in this tutorial. here I just show for making easy understand of code.
  1. if(!isset($_GET['page'])){
  2. $page = 1;
  3. } else {
  4. $page = $_GET['page'];
  5. }

Step 7 : Setting for start and end record to display on page.

Now will set LIMIT for mySQLi select query.
mySQLi  LIMIT its take two parameter first is OFFSET and second is range (first is optional). we provide  only one parameter it will show only first record to desire result .
Example
“SELECT * FROM data LIMIT 5”;
It will display first five records from data table.
On the other hand the OFFSET is use to display from particular records.
“SELECT * FROM data LIMIT 5, 5”;
It will display from sixth record to next five record on webpage (indexing starts from zero (0).
Setting for OFFSET
Suppose user clicked on page number 1, so we have to show the second next 5 records on page.
Lets calculate
$start = (1-1) * 5      // display 5 records per page
then $start will 0, so we have to display records from 0 to next 5.
if user clicked on page number 2 then
$start = (2-1)*5 = 5,  we have to display records from 5 to next 5
…. so on.
  1. $start = ($page-1)*$records_per_page;

Step 8 & 9 : Retrieve selected result on page and close the database connection.

  1. $selected_record = "SELECT * FROM data LIMIT " .$start.','.$records_per_page;
  2. $selected_data = mysqli_query($conn, $selected_record);
Now we have to fetch the selected records on webpage by running a while loop.
  1. <div style="height: 150px;">
  2. <?php
  3. while($row = mysqli_fetch_array($selected_data)):
  4. echo $row['id']. " " . $row['Alphabets']. '<br>';
  5. endwhile;
  6. // close the database connection as we don't requie remain open any more.
  7. mysqli_close($conn);
  8. ?>
  9. </div>

Step 10 : Making page links.

Creating Previous Link
  1. if($page > 1){
  2. $prv = $page - 1;
  3. echo '<span><a href="pagination.php?page='.$prv.'" class="page">Previous</a></span>';
  4. }
This code execute the previous link when user user visits greater then page number 1 like 2, 3 and so on.
Creating Page Link
  1. for($page=1; $page<=$total_page;$page++){
  2. if($page == $_GET['page'])
  3. {
  4. // on current page we do not need link
  5. echo "<span class='a'>".$page."</span>";
  6. }else
  7. {
  8. echo '<a href="pagination.php?page='.$page.'"> ' . $page . ' </a>';
  9. }
  10. }
Creating Next Link
  1. $check = $start + $records_per_page;
  2. if($total_records > $check)
  3. {
  4. if(!isset($_GET['page']))
  5. {
  6. $nxt = 2;
  7. }else
  8. {
  9. $nxt = $_GET['page'] + 1;
  10. }
  11. echo '<a href="pagination.php?page='.$nxt.'" class="page">Next</a>';
  12. }
This code will execute only when user visits on less than last page other wise it will remain disappear.

No comments:

Post a Comment

Upload file using PHP

Upload file using PHP This is a simple tutorial to learn how to upload file using PHP with the help of HTML form. PHP makes it very...