How to Build a Paging System Web Browser Interface

How to Build a Paging System Web Browser Interface thumbnail
Add a paging system to make it easier to browse through a website.

A paging system is used on a website when many records are returned from a database and they won't fit on a page without excessive scrolling. You see this method used in search engines and ecommerce sites. Adding a paging system will make it easier for the user to browse through the products on the page.

Things You'll Need

  • HTML Editor
  • MySQL
Show More

Instructions

    • 1

      Open at HTML editor (such as Notepad) by clicking on "Start," adding "Notepad" to the search box that appears and clicking "Ok".

    • 2

      Create a PHP page called products.php. This will display a list of pages containing products.

    • 3

      Set variables for the number of results per page and name of the page (copy and paste the following code):

      $PSYS_RESPP = 20;

      $PSYS_DEFURL = "tmtm.php";

    • 4

      Get the number of products in database:

      $PSYS_COUNT = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) FROM `products`;"));

    • 5

      Store the current page number and escape any invalid characters:

      $PSYS_PAGENUM = $_GET["page"];

      $PSYS_PAGENUM = mysql_real_escape_string($PSYS_PAGENUM);

    • 6

      If there is no page number set then set the page to 1 as you are on the first page:

      if (!$PSYS_PAGENUM || $PSYS_PAGENUM < 1 || $PSYS_PAGENUM > ($PSYS_COUNT["COUNT(*)"] / $PSYS_RESPP) + 1)

      $PSYS_PAGENUM = 1;

    • 7

      Display the list of products for the page:

      $strSQL = "SELECT * FROM products LIMIT $PSYS_PAGENUM, $PSYS_RESPP";

      $result = mysql_query("$strSQL") or die(mysql_error());

      while(list($productname)= mysql_fetch_row($result))

      { echo $productname;

      }

      The variables $PSYS_PAGENUM and $PSYS_RESPP set the start and end products for the individual page.

    • 8

      Set the link to the previous page of products, when scrolling through the pages:

      if (($PSYS_PAGENUM * $PSYS_RESPP) - ($PSYS_RESPP * 2) >= 0)

      echo "<a href=\"" . $PSYS_DEFURL . "?page=" . ($PSYS_PAGENUM - 1) . "\">« Previous</a> |";

      else

      echo "« Previous |";

    • 9

      Create the page numbers and output:

      for ($PSYS_TEMP = 1; ($PSYS_TEMP * $PSYS_RESPP) - $PSYS_RESPP < $PSYS_COUNT["COUNT(*)"]; $PSYS_TEMP++)

      {

      if ($PSYS_TEMP == $PSYS_PAGENUM)

      echo " " . $PSYS_TEMP . " |";

      else

      echo " <a href=\"" . $PSYS_DEFURL . "?page=" . $PSYS_TEMP . "\">" . $PSYS_TEMP . "</a> |";

      }

    • 10

      Add the link to take user to the next page of products:

      if ($PSYS_PAGENUM * 20 < $PSYS_COUNT["COUNT(*)"])

      echo " <a href=\"" . $PSYS_DEFURL . "?page=" . ($PSYS_PAGENUM + 1) . "\">Next »</a>";

      else

      echo " Next »";

    • 11

      Set up the variable to contain the next page number:

      $PSYS_PAGENUM = ($PSYS_PAGENUM * $PSYS_RESPP) - $PSYS_RESPP;

    • 12

      Save the file and run in your browser. You will see a list of pages numbered from 1 upwards and "Previous" and "Next" links to connect to other pages.

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured