Guest User

Untitled

a guest
Jan 2nd, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. if(!function_exists("ShowRecords")) {
  2. function ShowRecords($sql_query, $table) {
  3. global $conn;
  4.  
  5. $button_display='';
  6.  
  7. $query_string = '?';
  8. foreach($_GET as $q => $k) {
  9. if($q == 'id' or $q == 'pagenum') {
  10. //
  11. } else {
  12. if($q != '') {
  13. $query_string = '&'.$q.'='.$k;
  14. }
  15. }
  16. }
  17.  
  18. // Find total number of rows in table
  19. //$sql="SELECT COUNT(*) FROM $table";
  20. $sql = $sql_query;
  21. $rs = mysql_query($sql,$conn);
  22. if(mysql_num_rows($rs) > 0) {
  23. $total_rows = mysql_num_rows($rs);
  24.  
  25. // Set rows per page
  26. if($total_rows < 25) {
  27. $rows_per_page = $total_rows;
  28. } else {
  29. $rows_per_page = 25;
  30. }
  31.  
  32. // Calculate total number of pages
  33. $total_pages = ceil($total_rows / $rows_per_page);
  34.  
  35. // Get current page
  36. $current_page = (isset($_GET['pagenum']) && $_GET['pagenum'] > 0) ? (int) $_GET['pagenum'] : 1;
  37.  
  38. // If current page is greater than last page, set it to last.
  39. if ($current_page > $total_pages)
  40. $current_page = $total_pages;
  41.  
  42. // Set starting post
  43. $offset = ($current_page - 1) * $rows_per_page;
  44.  
  45. // Get rows from database
  46. $sql = $sql_query.' LIMIT '.$offset.', '.$rows_per_page;
  47. //$sql="SELECT * FROM $table LIMIT $offset, $rows_per_page";
  48. $rs = mysql_query($sql,$conn) or die(mysql_error());
  49. //return rows
  50. $results = array();
  51. while($result = mysql_fetch_array($rs)) {
  52. $results[] = $result;
  53. //echo $result["company"].'<br>';
  54. }
  55. //return $results;
  56.  
  57. // Build navigation
  58. // Range of pages on each side of current page in navigation
  59. $range = 4;
  60.  
  61. //if the total number of pages are less then 20 then display all page numbers
  62. //otherwise display a certain range of pages in between with ...<LastPageNumber>
  63. unset($_GET["pagenum"]);
  64. if($total_pages > 20) {
  65. //Previous page and first page
  66. if ($current_page > 1) {
  67. $back = $current_page - 1;
  68. $button_display.= ' <input type="button" onClick="parent.location=\'?'.$query_string.'&pagenum='.$back.'\'" value="Previous" /> ';
  69. if($current_page > ($range + 1)) {
  70. $button_display.= ' <input type="button" onClick="parent.location=\'?'.$query_string.'&pagenum=1\'" value="1" /> ... ';
  71. }
  72. } else {
  73. $button_display.= ' <input type="button" value="Previous" disabled="disabled" /> ';
  74. }
  75.  
  76. //numbers between
  77. for($i = $current_page - $range; $i < ($current_page + $range) + 1; $i++) {
  78. if($i > 0 && $i <= $total_pages) {
  79. if($i == $current_page) {
  80. $button_display.= ' <input type="button" value="'.$i.'" class="active" /> ';
  81. } else {
  82. $button_display.= ' <input type="button" onClick="parent.location=\'?'.$query_string.'&pagenum='.$i.'\'" value="'.$i.'" /> ';
  83. }
  84. }
  85. }
  86. //next and last page
  87. if($current_page != $total_pages) {
  88. $next = $current_page + 1;
  89. if(($current_page + $range) < $total_pages) {
  90. $button_display.= ' ... <input type="button" onClick="parent.location=\'?'.$query_string.'&pagenum='.$total_pages.'\'" value="'.$total_pages.'" /> ';
  91. }
  92. $button_display.= ' <input type="button" onClick="parent.location=\'?'.$query_string.'&pagenum='.$next.'\'" value="Next" /> ';
  93. } else {
  94. $button_display.= ' <input type="button" value="Next" disabled="disabled" /> ';
  95. }
  96. } else {
  97. //Previous page
  98. if ($current_page > 1) {
  99. $back = $current_page - 1;
  100. $button_display.= ' <input type="button" onClick="parent.location=\'?'.$query_string.'&pagenum='.$back.'\'" value="Previous" /> ';
  101. } else {
  102. $button_display.= ' <input type="button" value="Previous" disabled="disabled" /> ';
  103. }
  104.  
  105. //numbers between
  106. for($i = 0; $i < ($total_pages) + 1; $i++) {
  107. if($i > 0 && $i <= $total_pages) {
  108. if($i == $current_page) {
  109. $button_display.= ' <input type="button" value="'.$i.'" class="active" /> ';
  110. } else {
  111. $button_display.= ' <input type="button" onClick="parent.location=\'?'.$query_string.'&pagenum='.$i.'\'" value="'.$i.'" /> ';
  112. }
  113. }
  114. }
  115. //next button
  116. if($current_page != $total_pages) {
  117. $next = $current_page + 1;
  118. $button_display.= ' <input type="button" onClick="parent.location=\'?'.$query_string.'&pagenum='.$next.'\'" value="Next" /> ';
  119. } else {
  120. $button_display.= ' <input type="button" value="Next" disabled="disabled" /> ';
  121. }
  122. }
  123.  
  124. $ShowingFrom = ($current_page-1) * $rows_per_page + 1;
  125. $ShowintTo = $current_page * $rows_per_page;
  126.  
  127. $table_display = '<table width="100%" border="0" cellspacing="10" cellpadding="10">
  128. <tr>
  129. <td align="left"><h4>Showing Records '.$ShowingFrom.' to '.$ShowintTo.' of '.$total_rows.'</h4></td>
  130. <td align="right">'.$button_display.'</td>
  131. </tr>
  132. </table>';
  133.  
  134. return array("results" => $results, "PageNavi" => $table_display);
  135. } else {
  136. //no results
  137. }
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment