Advertisement
melody45

ps-pagination

Mar 25th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class PS_Pagination {
  5. var $php_self;
  6. var $rows_per_page = 10; //Number of records to display per page
  7. var $total_rows = 0; //Total number of rows returned by the query
  8. var $links_per_page = 5; //Number of links to display per page
  9. var $append = ""; //Paremeters to append to pagination links
  10. var $sql = "";
  11. var $debug = false;
  12. var $conn = false;
  13. var $page = 1;
  14. var $max_pages = 0;
  15. var $offset = 0;
  16.  
  17. /**
  18. * Constructor
  19. *
  20. * @param resource $connection Mysql connection link
  21. * @param string $sql SQL query to paginate. Example : SELECT * FROM users
  22. * @param integer $rows_per_page Number of records to display per page. Defaults to 10
  23. * @param integer $links_per_page Number of links to display per page. Defaults to 5
  24. * @param string $append Parameters to be appended to pagination links
  25. */
  26.  
  27. function PS_Pagination($connection, $sql, $rows_per_page = 10, $links_per_page = 5, $append = "") {
  28. $this->conn = $connection;
  29. $this->sql = $sql;
  30. $this->rows_per_page = (int)$rows_per_page;
  31. if (intval($links_per_page ) > 0) {
  32. $this->links_per_page = (int)$links_per_page;
  33. } else {
  34. $this->links_per_page = 5;
  35. }
  36. $this->append = $append;
  37. $this->php_self = htmlspecialchars($_SERVER['PHP_SELF'] );
  38. if (isset($_GET['page'] )) {
  39. $this->page = intval($_GET['page'] );
  40. }
  41. }
  42.  
  43. /**
  44. * Executes the SQL query and initializes internal variables
  45. *
  46. * @access public
  47. * @return resource
  48. */
  49. function paginate() {
  50. //Check for valid mysql connection
  51. if (! $this->conn || ! is_resource($this->conn )) {
  52. if ($this->debug)
  53. echo "MySQL connection missing<br />";
  54. return false;
  55. }
  56.  
  57. //Find total number of rows
  58.  
  59. $all_rs = @mysql_query($this->sql );
  60. if (! $all_rs) {
  61. if ($this->debug)
  62. echo "SQL query failed. Check your query.<br /><br />Error Returned: " . mysql_error();
  63. return false;
  64. }
  65. $this->total_rows = mysql_num_rows($all_rs );
  66. @mysql_close($all_rs );
  67.  
  68. //Return FALSE if no rows found
  69. if ($this->total_rows == 0) {
  70. if ($this->debug)
  71. //echo "No Record Found.";
  72. return FALSE;
  73. }
  74.  
  75. //Max number of pages
  76. $this->max_pages = ceil($this->total_rows / $this->rows_per_page );
  77. if ($this->links_per_page > $this->max_pages) {
  78. $this->links_per_page = $this->max_pages;
  79. }
  80.  
  81. //Check the page value just in case someone is trying to input an aribitrary value
  82. if ($this->page > $this->max_pages || $this->page <= 0) {
  83. $this->page = 1;
  84. }
  85.  
  86. //Calculate Offset
  87. $this->offset = $this->rows_per_page * ($this->page - 1);
  88.  
  89. //Fetch the required result set
  90. $rs = @mysql_query($this->sql . " LIMIT {$this->offset}, {$this->rows_per_page}" );
  91. if (! $rs) {
  92. if ($this->debug)
  93. echo "Pagination query failed. Check your query.<br /><br />Error Returned: " . mysql_error();
  94. return false;
  95. }
  96. return $rs;
  97. }
  98.  
  99. /**
  100. * Display the link to the first page
  101. *
  102. * @access public
  103. * @param string $tag Text string to be displayed as the link. Defaults to 'First'
  104. * @return string
  105. */
  106. function renderFirst($tag = '<img src="images/first.png" >') {
  107. if ($this->total_rows == 0)
  108. return FALSE;
  109.  
  110. if ($this->page == 1) {
  111. return "$tag ";
  112. } else {
  113. return '<a href="' . $this->php_self . '?page=1&' . $this->append . '">' . $tag . '</a> ';
  114. }
  115. }
  116.  
  117. /**
  118. * Display the link to the last page
  119. *
  120. * @access public
  121. * @param string $tag Text string to be displayed as the link. Defaults to 'Last'
  122. * @return string
  123. */
  124. function renderLast($tag = '<img src="images/last.png" >') {
  125. if ($this->total_rows == 0)
  126. return FALSE;
  127.  
  128. if ($this->page == $this->max_pages) {
  129. return $tag;
  130. } else {
  131. return ' <a href="' . $this->php_self . '?page=' . $this->max_pages . '&' . $this->append . '">' . $tag . '</a>';
  132. }
  133. }
  134.  
  135. /**
  136. * Display the next link
  137. *
  138. * @access public
  139. * @param string $tag Text string to be displayed as the link. Defaults to '>>'
  140. * @return string
  141. */
  142. function renderNext($tag = '<img src="images/next_btn.gif" style="border:none" height="8" width="8">') {
  143. if ($this->total_rows == 0)
  144. return FALSE;
  145.  
  146. if ($this->page < $this->max_pages) {
  147. return '<a href="' . $this->php_self . '?page=' . ($this->page + 1) . '&' . $this->append . '"><img src="images/next_btn.gif" style="border:none" height="8" width="8"></a>';
  148. } else {
  149. return $tag;
  150. }
  151. }
  152.  
  153. /**
  154. * Display the previous link
  155. *
  156. * @access public
  157. * @param string $tag Text string to be displayed as the link. Defaults to '<<'
  158. * @return string
  159. */
  160. function renderPrev($tag = '<img src="images/back_btn2.gif" style="border:none" height="8" width="8">') {
  161. if ($this->total_rows == 0)
  162. return FALSE;
  163.  
  164. if ($this->page > 1) {
  165. return ' <a href="' . $this->php_self . '?page=' . ($this->page - 1) . '&' . $this->append . '"><img src="images/back_btn2.gif" style="border:none" height="8" width="8"></a>';
  166. } else {
  167. return " $tag";
  168. }
  169. }
  170.  
  171. /**
  172. * Display the page links
  173. *
  174. * @access public
  175. * @return string
  176. */
  177. function renderNav($prefix = '<span class="page_link">', $suffix = '</span>') {
  178. if ($this->total_rows == 0)
  179. return FALSE;
  180.  
  181. $batch = ceil($this->page / $this->links_per_page );
  182. $end = $batch * $this->links_per_page;
  183. if ($end == $this->page) {
  184. //$end = $end + $this->links_per_page - 1;
  185. //$end = $end + ceil($this->links_per_page/2);
  186. }
  187. if ($end > $this->max_pages) {
  188. $end = $this->max_pages;
  189. }
  190. $start = $end - $this->links_per_page + 1;
  191. $links = '';
  192.  
  193. for($i = $start; $i <= $end; $i ++) {
  194. if ($i == $this->page) {
  195. $links .= $prefix . " $i " . $suffix;
  196. } else {
  197. $links .= ' ' . $prefix . '<a href="' . $this->php_self . '?page=' . $i . '&' . $this->append . '">' . $i . '</a>' . $suffix . ' ';
  198. }
  199. }
  200.  
  201. return $links;
  202. }
  203.  
  204. /**
  205. * Display full pagination navigation
  206. *
  207. * @access public
  208. * @return string
  209. */
  210. function renderFullNav() {
  211. return $this->renderFirst() . '&nbsp;' . $this->renderPrev() . '&nbsp;' . $this->renderNav() . '&nbsp;' . $this->renderNext() . '&nbsp;' . $this->renderLast();
  212. }
  213.  
  214. /**
  215. * Set debug mode
  216. *
  217. * @access public
  218. * @param bool $debug Set to TRUE to enable debug messages
  219. * @return void
  220. */
  221. function setDebug($debug) {
  222. $this->debug = $debug;
  223. }
  224. }
  225. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement