Advertisement
Guest User

Untitled

a guest
Nov 1st, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. <html>
  2.  
  3. <head>
  4. <title>Paging Using PHP</title>
  5. </head>
  6.  
  7. <body>
  8. <?php
  9. $dbhost = 'localhost:3036';
  10. $dbuser = 'root';
  11. $dbpass = 'rootpassword';
  12.  
  13. $rec_limit = 10;
  14. $conn = mysql_connect($dbhost, $dbuser, $dbpass);
  15.  
  16. if(! $conn ) {
  17. die('Could not connect: ' . mysql_error());
  18. }
  19. mysql_select_db('test_db');
  20.  
  21. /* Get total number of records */
  22. $sql = "SELECT count(emp_id) FROM employee ";
  23. $retval = mysql_query( $sql, $conn );
  24.  
  25. if(! $retval ) {
  26. die('Could not get data: ' . mysql_error());
  27. }
  28. $row = mysql_fetch_array($retval, MYSQL_NUM );
  29. $rec_count = $row[0];
  30.  
  31. if( isset($_GET{'page'} ) ) {
  32. $page = $_GET{'page'} + 1;
  33. $offset = $rec_limit * $page ;
  34. }else {
  35. $page = 0;
  36. $offset = 0;
  37. }
  38.  
  39. $left_rec = $rec_count - ($page * $rec_limit);
  40. $sql = "SELECT emp_id, emp_name, emp_salary ".
  41. "FROM employee ".
  42. "LIMIT $offset, $rec_limit";
  43.  
  44. $retval = mysql_query( $sql, $conn );
  45.  
  46. if(! $retval ) {
  47. die('Could not get data: ' . mysql_error());
  48. }
  49.  
  50. while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
  51. echo "EMP ID :{$row['emp_id']} <br> ".
  52. "EMP NAME : {$row['emp_name']} <br> ".
  53. EMP SALARY : {$row['emp_salary']} <br> ".
  54. "--------------------------------<br>";
  55. }
  56.  
  57. if( $page > 0 ) {
  58. $last = $page - 2;
  59. echo "<a href = \"$_PHP_SELF?page = $last\">Last 10 Records</a> |";
  60. echo "<a href = \"$_PHP_SELF?page = $page\">Next 10 Records</a>";
  61. }else if( $page == 0 ) {
  62. echo "<a href = \"$_PHP_SELF?page = $page\">Next 10 Records</a>";
  63. }else if( $left_rec < $rec_limit ) {
  64. $last = $page - 2;
  65. echo "<a href = \"$_PHP_SELF?page = $last\">Last 10 Records</a>";
  66. }
  67.  
  68. mysql_close($conn);
  69. ?>
  70.  
  71. </body>
  72. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement