PageRenderTime 30ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/lib/eleontev/PageByPage.php

https://bitbucket.org/iarp/knowledgebase
PHP | 348 lines | 208 code | 95 blank | 45 comment | 32 complexity | e73f54318d6561e3195c19383585a97d MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | Author: Evgeny Leontev <eleontev@gmail.com> |
  4. // | Copyright (c) 2005 Evgeny Leontev |
  5. // +----------------------------------------------------------------------+
  6. // | This source file is free software; you can redistribute it and/or |
  7. // | modify it under the terms of the GNU Lesser General Public |
  8. // | License as published by the Free Software Foundation; either |
  9. // | version 2.1 of the License, or (at your option) any later version. |
  10. // | |
  11. // | This source file is distributed in the hope that it will be useful, |
  12. // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  13. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
  14. // | Lesser General Public License for more details. |
  15. // +----------------------------------------------------------------------+
  16. /**
  17. * PageByPage is a class used to create page by page navigation.
  18. *
  19. * @since 20/05/2003
  20. * @author Evgeny Leontev <eleontev@gmail.com>
  21. * @access public
  22. */
  23. class PageByPage {
  24. //var $form_action;
  25. var $hidden; // values to be in get string
  26. var $query;
  27. var $get_name = 'bp'; // $_GET name to be in query
  28. var $get_delim = '|';
  29. var $action_page = '';
  30. var $offset = 0; // mysql offset
  31. var $limit = 10; // mysql offset
  32. var $num_records; // num records
  33. var $num_pages; // num pages
  34. var $cur_record = 1; // number of record
  35. var $cur_page = 1; // number of page
  36. var $nav;
  37. var $use_form = true;
  38. var $page_msg = 'Page';
  39. var $record_msg = 'Records';
  40. var $record_from_msg = 'from';
  41. var $per_page_msg = 'Records per page';
  42. var $per_page_name = 'records_per_page';
  43. var $per_page_range = array(10, 20, 40);
  44. function PageByPage($limit = false, $hidden = false) {
  45. if($limit) { $this->limit = $limit; }
  46. if($hidden) { $this->_setVars($hidden); }
  47. // overwrite limit
  48. if(isset($_GET[$this->per_page_name])) {
  49. $this->limit = $_GET[$this->per_page_name];
  50. }
  51. }
  52. // to use get vars in navigation
  53. function _setVars($arr) {
  54. unset($arr[$this->get_name], $arr['submit']);
  55. $this->hidden = http_build_hidden($arr);
  56. $this->query = '&' . http_build_query($arr);
  57. }
  58. // using COUNT() for count, use it in $sql
  59. function countAll($sql, $db_obj = false){
  60. if(!isset($_GET[$this->get_name])) {
  61. if(is_numeric($sql)) {
  62. $this->num_records = $sql; // num records
  63. } else {
  64. $sql = preg_replace("#SELECT\s{1,}(.+?)\s{1,}FROM# is", 'SELECT COUNT(*) FROM', $sql);
  65. //echo "<pre>"; print_r($sql); echo "</pre>";
  66. if($db_obj) {
  67. $val = $db_obj->GetCol($sql) or die (db_error($sql));
  68. $this->num_records = $val[0];
  69. } else {
  70. $result = mysql_query($sql) or die (db_error($sql));
  71. $this->num_records = @mysql_result($result,0); // num records
  72. }
  73. }
  74. }
  75. //echo "<pre>"; print_r($sql); echo "</pre>";
  76. return $this->_prepare();
  77. }
  78. // set sql with limit
  79. function setLimitSql($sql){
  80. $sql .= sprintf(" LIMIT %d, %d", $this->offset, $this->limit);
  81. return $sql;
  82. }
  83. // using mysql_num_rows() for count
  84. function countReal($sql, $db_obj = false){
  85. if(!isset($_GET[$this->get_name])) {
  86. $result = mysql_query($sql) or die (db_error());
  87. $this->num_records = mysql_num_rows($result);
  88. }
  89. return $this->_prepare();
  90. }
  91. function _prepare() {
  92. if(!isset($_GET[$this->get_name])) {
  93. $this->num_pages = ceil($this->num_records/$this->limit); //num pages
  94. } else {
  95. list($this->offset, $this->num_records, $this->num_pages, $this->cur_record, $this->cur_page) = explode($this->get_delim, urldecode($_GET[$this->get_name]));
  96. }
  97. $sql_limit['offset'] = $this->offset;
  98. $sql_limit['limit'] = $this->limit;
  99. return $sql_limit;
  100. }
  101. function _pages(){
  102. if(!$this->action_page) {
  103. $this->action_page = $_SERVER['PHP_SELF'];
  104. }
  105. $a = sprintf('<form action="%s" name="by_page" style="margin : 0px 0px;">', $this->action_page);
  106. $a .= $this->page_msg . ": ";
  107. if($this->use_form == false) {
  108. $a .= $this->cur_page . ' ';
  109. $a .= $this->record_from_msg . ' ' . $this->num_pages;
  110. } elseif($this->num_pages > 1){
  111. $a .= sprintf('<select name="%s" style="padding: 0px; width: 45; font: 10px;" onchange="document.by_page.submit();">', $this->get_name);
  112. for($i=1;$i<=$this->num_pages;$i++) {
  113. if($this->cur_page == $i) {
  114. $a .= '<option value="" selected="selected">'.$i.'</option>';
  115. } else {
  116. $bp = $i*$this->limit-$this->limit . $this->get_delim;
  117. $bp .= $this->num_records . $this->get_delim;
  118. $bp .= $this->num_pages . $this->get_delim;
  119. $bp .= ($i*$this->limit-$this->limit+1) . $this->get_delim;
  120. $bp .= $i;
  121. $a .= '<option value="'.$bp.'">'.$i.'</option>';
  122. }
  123. }
  124. $a .= '</select> ';
  125. $a .= $this->record_from_msg . ' ' . $this->num_pages;
  126. } else {
  127. $a .= 1;
  128. }
  129. $sign = (strpos($this->action_page, '?') !== false) ? '&' : '?';
  130. // print previos arows (<<)
  131. if($this->num_pages > 1 && $this->cur_page != 1){
  132. $bp = $this->offset-$this->limit . $this->get_delim; // offset
  133. $bp .= $this->num_records . $this->get_delim; //
  134. $bp .= $this->num_pages . $this->get_delim;
  135. $bp .= $this->cur_record-$this->limit . $this->get_delim; // cur record when numerate records
  136. $bp .= $this->cur_page-1; // cur page
  137. $a .= '&nbsp;&nbsp;<a href="'.$this->action_page.$sign.$this->get_name.'='.$bp.$this->query.'" title="previos"><b>&laquo;&laquo;</b></a>';
  138. }
  139. // print next arrows (>>)
  140. if($this->num_pages > $this->cur_page){
  141. $bp = $this->limit*$this->cur_page . $this->get_delim;
  142. $bp .= $this->num_records . $this->get_delim;
  143. $bp .= $this->num_pages . $this->get_delim;
  144. $bp .= ($this->cur_page*$this->limit+1) . $this->get_delim;
  145. $bp .= $this->cur_page+1;
  146. $a .= '&nbsp;&nbsp;<a href="'.$this->action_page.$sign.$this->get_name.'='.$bp.$this->query.'" title="next"><b>&raquo;&raquo;</b></a>';
  147. }
  148. $a .= $this->hidden; // hidden fields
  149. $a .= '</form>';
  150. return $a;
  151. }
  152. function _records(){
  153. $n = $this->limit*$this->cur_page;
  154. if($n > $this->num_records){
  155. $b = ($n - $this->num_records);
  156. $n = ($n - $b);
  157. }
  158. return $this->record_msg . ": ".($this->offset+1)."-".$n." ".$this->record_from_msg." ".$this->num_records;
  159. }
  160. // set msg, we should create 3 msg
  161. // for example: setMsg(Pages, Records, from)
  162. function setMsg($msg) {
  163. if(!is_array($msg)) { $msg = func_get_args(); }
  164. $this->page_msg = $msg[0];
  165. $this->record_msg = $msg[1];
  166. $this->record_from_msg = $msg[2];
  167. }
  168. function setPerPageMsg($msg) {
  169. $this->per_page_msg = $msg;
  170. }
  171. function setPerPageRange($arr) {
  172. $this->per_page_range = array();
  173. foreach($arr as $v) {
  174. $this->per_page_range[$v] = $v;
  175. }
  176. }
  177. // to print page by page line
  178. function navigate($more_fields = false){
  179. if($more_fields !== false) {
  180. require_once ('HTML/FormSelect.php');
  181. $this->setPerPageRange($this->per_page_range);
  182. $select = new FormSelect();
  183. $select->setFormMethod($_GET);
  184. $select->setFormName('per_page_form');
  185. $select->setSelectWidth(50);
  186. $select->setOnChangeSubmit();
  187. $select->setSelectName($this->per_page_name);
  188. $select->setRange($this->per_page_range);
  189. $sel_html = '<form action="" style="margin: 0px;">';
  190. $limit = (isset($_GET[$this->per_page_name])) ? $_GET[$this->per_page_name] : $this->limit;
  191. $sel_html .= $select->select($limit);
  192. $vars = $_GET;
  193. unset($vars[$this->per_page_name]);
  194. $sel_html .= http_build_hidden($vars);
  195. $sel_html .= '</form>';
  196. $sel_html = '<td style="white-space: nowrap;">&nbsp;' . $this->per_page_msg . ':</td>
  197. <td>'.$sel_html.'</td>';
  198. } else {
  199. $sel_html = '';
  200. }
  201. $html = '<table cellpadding="4" cellspacing="0" border="0" width="100%">
  202. <tr bgcolor="#e7e7e7">
  203. <td width="100%">'.$this->_records().'</td>
  204. <td nowrap align="right" style="padding-right: 15px;">'.$this->_pages().'</td>
  205. <td bgcolor="#ffffff" style="padding: 1px;"></td>
  206. ' . $sel_html . '
  207. </tr>
  208. </table>';
  209. return $html;
  210. }
  211. function _pages1() {
  212. if(!$this->action_page) {
  213. $this->action_page = $_SERVER['PHP_SELF'];
  214. }
  215. $sign = (strpos($this->action_page, '?') !== false) ? '&' : '?';
  216. $a = '';
  217. //$a .= $this->page_msg . ": ";
  218. // print previos arows (<<)
  219. if($this->num_pages > 1 && $this->cur_page != 1){
  220. $bp = $this->offset-$this->limit . $this->get_delim; // offset
  221. $bp .= $this->num_records . $this->get_delim; //
  222. $bp .= $this->num_pages . $this->get_delim;
  223. $bp .= $this->cur_record-$this->limit . $this->get_delim; // cur record when numerate records
  224. $bp .= $this->cur_page-1; // cur page
  225. $a .= '&nbsp;&nbsp;<a href="'.$this->action_page.$sign.$this->get_name.'='.$bp.$this->query.'" title="Previous"><b>&laquo;&laquo;</b></a>';
  226. }
  227. // print next arrows (>>)
  228. if($this->num_pages > $this->cur_page){
  229. $bp = $this->limit*$this->cur_page . $this->get_delim;
  230. $bp .= $this->num_records . $this->get_delim;
  231. $bp .= $this->num_pages . $this->get_delim;
  232. $bp .= ($this->cur_page*$this->limit+1) . $this->get_delim;
  233. $bp .= $this->cur_page+1;
  234. $a .= '&nbsp;&nbsp;<a href="'.$this->action_page.$sign.$this->get_name.'='.$bp.$this->query.'" title="next"><b>&raquo;&raquo;</b></a>';
  235. }
  236. return $a;
  237. }
  238. function shortNavigate() {
  239. return sprintf('%s&nbsp;&nbsp;%s', $this->_records(), $this->_pages1());
  240. }
  241. }
  242. // Showing 1 to 10 of 177 < Previous Next >
  243. /*
  244. $bp = new PageByPage(10, $_GET);
  245. $bp->countAll(15);
  246. echo $bp->shortNavigate();
  247. echo "<pre>"; print_r($bp); echo "</pre>";
  248. */
  249. ?>