Paging in PHP

This sections will contains code regarding different PHP & MySQL features and contains help about php/mysql issues.

Paging in PHP

Postby Web Guru on August 14th, 2008, 2:12 am

Below is the code that shows how to implement the paging in PHP. It is very simple. You can easily customize that code as per your requirements.

Paging Demo:
Code: Select all
<html>

<head>
   <title>Pagination Demo</title>
</head>

<body>
<?
   $sData = array( );

   $sData[] = array(1, "text 1", "photo 1", "video 1");
   $sData[] = array(2, "text 2", "photo 2", "video 2");
   $sData[] = array(3, "text 3", "photo 3", "video 3");
   $sData[] = array(4, "text 4", "photo 4", "video 4");
   $sData[] = array(5, "text 5", "photo 5", "video 5");
   $sData[] = array(6, "text 6", "photo 6", "video 6");
   $sData[] = array(7, "text 7", "photo 7", "video 7");
   $sData[] = array(8, "text 8", "photo 8", "video 8");
   $sData[] = array(9, "text 9", "photo 9", "video 9");
   $sData[] = array(10, "text 10", "photo 10", "video 10");
   $sData[] = array(11, "text 11", "photo 11", "video 11");
   $sData[] = array(12, "text 12", "photo 12", "video 12");
   $sData[] = array(13, "text 13", "photo 13", "video 13");
   $sData[] = array(14, "text 14", "photo 14", "video 14");
   $sData[] = array(15, "text 15", "photo 15", "video 15");
   $sData[] = array(16, "text 16", "photo 16", "video 16");
   $sData[] = array(17, "text 17", "photo 17", "video 17");
   $sData[] = array(18, "text 18", "photo 18", "video 18");
   $sData[] = array(19, "text 19", "photo 19", "video 19");
   $sData[] = array(20, "text 20", "photo 20", "video 20");
   $sData[] = array(21, "text 21", "photo 21", "video 21");
   $sData[] = array(22, "text 22", "photo 22", "video 22");
   $sData[] = array(23, "text 23", "photo 23", "video 23");
   $sData[] = array(24, "text 24", "photo 24", "video 24");
   $sData[] = array(25, "text 25", "photo 25", "video 25");
   $sData[] = array(26, "text 26", "photo 26", "video 26");
   $sData[] = array(27, "text 27", "photo 27", "video 27");
   $sData[] = array(28, "text 28", "photo 28", "video 28");
   $sData[] = array(29, "text 29", "photo 29", "video 29");
   $sData[] = array(30, "text 30", "photo 30", "video 30");

   $iTotalRecords = count($sData);


   $iPageId = intval($_REQUEST['PageId']);
   $iPageId = (($iPageId == 0) ? 1 : $iPageId);

   $iMaxRecords = intval($_REQUEST['MaxRecords']);
   $iMaxRecords = (($iMaxRecords == 0) ? 10 : $iMaxRecords);

   $iStart = (($iPageId * $iMaxRecords) - $iMaxRecords);
   $iEnd   = ($iPageId * $iMaxRecords);

   if ($iEnd > $iTotalRecords)
      $iEnd = $iTotalRecords;

   $iPageCount = @ceil($iTotalRecords / $iMaxRecords);

//   if (($iTotalRecords % $iMaxRecords) != 0)
//      $iPageCount ++;

?>
   <b>Current Page # <?= $iPageId ?><br />
   <br />

   <table border="1" cellspacing="2" cellpadding="3" width="500">
      <tr>
         <td><b>#</b></td>
         <td><b>Text</b></td>
         <td><b>Photo</b></td>
         <td><b>Video</b></td>
      </tr>

<?
   for ($i = $iStart; $i < $iEnd; $i ++)
   {
?>
      <tr>
         <td><?= $sData[$i][0] ?></td>
         <td><?= $sData[$i][1] ?></td>
         <td><?= $sData[$i][2] ?></td>
         <td><?= $sData[$i][3] ?></td>
      </tr>
<?
   }
?>
   </table>

   Displaying <b><?= ($iStart + 1) ?></b> to <b><?= $iEnd ?></b> (of <b><?= $iTotalRecords ?></b> records)<br />
   <br />

   <table border="0" cellspacing="0" cellpadding="0" width="500">
      <tr>
         <td align="left"><input type="button" value="First Page" onclick="document.location='test.php?PageId=1&MaxRecords=<?= $iMaxRecords ?>';" <?= (($iPageId == 1) ? "disabled" : "") ?>></td>
         <td align="left"><input type="button" value="Previous Page" onclick="document.location='test.php?PageId=<?= ($iPageId - 1) ?>&MaxRecords=<?= $iMaxRecords ?>';" <?= (($iPageId == 1) ? "disabled" : "") ?>></td>
         <td align="center">Total Pages : <?= $iPageCount ?></td>
         <td align="right"><input type="button" value="Next Page" onclick="document.location='test.php?PageId=<?= ($iPageId + 1) ?>&MaxRecords=<?= $iMaxRecords ?>';" <?= (($iPageId == $iPageCount) ? "disabled" : "") ?>></td>
         <td align="right"><input type="button" value="Last Page" onclick="document.location='test.php?PageId=<?= $iPageCount ?>&MaxRecords=<?= $iMaxRecords ?>';" <?= (($iPageId == $iPageCount) ? "disabled" : "") ?>></td>
      </tr>
   </table>

   <br />

   <form method="get" action="test.php">
      <b>No of Records per page</b><br />
      <input type="text" name="MaxRecords" value="<?= $iMaxRecords ?>" size="3" maxlength="3" />
      <input type="submit" value="Set" />
   </form>

</body>
</html>
User avatar
Web Guru
 
Posts: 68
Joined: March 24th, 2008, 7:59 am
Location: Lahore, Pakistan

Return to PHP / MySQL / XML

Who is online

Users browsing this forum: No registered users and 0 guests

cron