How to Do Simple Pagination in PHP

From WikiHTP

In situations in which a query returns a lot of data, instead of sending them all to the browser, a limited set of records can be sent. Then, through hyperlinks, see the rest of the data. For example, when we search with the google server, it usually does not return all the addresses where the searched results are found, it returns us with 10 links per page (let's think about the transfer time if we return 1,000,000 links). So here we are about to see how to do Simple Pagination using PHP in your website.

Simple Pagination in PHP[edit]

Well, now we solve with PHP the problem of paging, we will show two students per page:

    <?php
      if(isset($_REQUEST['pos']))
        $start = $_REQUEST['pos'];
      else
        $start = 0;
    ?>
    <html>
      <head>
        <title> Pagination </title>
      </head>
      <body>
      <?php
        $connection = mysqli_connect ("localhost", "root", "", "base1") or
        die ("Problems with the connection");

        $records = mysqli_query ($connection, "select 
                                      id,
  				      first_name,
				      mail,
				      course
		                      from alumni limit $start, 2");
        $printed = 0;
        while($reg = mysqli_fetch_array ($records))
        {
          $printed++;
          echo "Id:". $reg ['id']. "" <br> ";
          echo "Name:". $ reg ['first_name']. "" <br> ";
          echo "Mail:". $ reg ['mail']. "" <br> ";
          echo "Course:". $ reg ['course']. "" <br> ";
          echo "<hr>";
        }
       if ($start == 0)
        echo "previous";
       else
       {
         $previous = $start-2;
           echo "<a href=\"page.php?pos=$previous\"> Previous </a>";
       }
       if($printed == 2)
       {
         $next = $start + 2;
         echo "<a href=\"page.php?pos=$next\"> Next </a>";
       }
       else
        echo "Next";
       mysqli_close ($ connection);
      ?>
     </body>
    </html>

The isset function returns true if there is a variable that we passed to it as a parameter, in this case we are passing the pos using $_REQUEST method.

When we call this page for the first time, we call it as page.php without parameters, so the if is verified as false. That is, the $start variable is loaded with the value 0.

Another important concept is the limit clause that is specific to the MySQL manager. Through this clause, we limit the number of records that the select returns. The first value of the limit indicates from which record and the second the number of records. That is, if a select without limit returns 100 records, then using, for example, the syntax limit 50,25 will return us, from that list of 100 records, from the record of position 50, 25 records.

   $records = mysqli_query ($connection, "select 
                                      id,
  				      first_name,
				      mail,
				      course
		                      from alumni limit $start, 2");

Then we show all the returned records and we also have them:

   while($reg = mysqli_fetch_array ($records))
        {
          $printed++;
          echo "Id:". $reg ['id']. "" <br> ";
          echo "Name:". $ reg ['first_name']. "" <br> ";
          echo "Mail:". $ reg ['mail']. "" <br> ";
          echo "Course:". $ reg ['course']. "" <br> ";
          echo "<hr>";
        }

Now we see where we will have the hyperlinks, forward or backward:

      if ($start == 0)
        echo "previous";
       else
       {
         $previous = $start-2;
           echo "<a href=\"page.php?pos=$previous\"> Previous </a>";
       }

If the $start variable is 0 it means that there are no records before it, so we only show a text "previous". In case the $start variable is different from 0, the else is executed, where we have a hyperlink with the same page and initializing the pos parameter with the value of $start minus 2.

If the counter $printed has the value 2 it means that there are possibly more records to be displayed and we must have a hyperlink with the same page but initializing the pos parameter with the value of $start plus 2.

    if($printed == 2)
       {
         $next = $start + 2;
         echo "<a href=\"page.php?pos=$next\"> Next </a>";
       }
       else
        echo "Next";

About This Tutorial

This page was last edited on 18 February 2019, at 21:05.