Posted By

mattkenefick on 05/20/08


Tagged

class php paging


Versions (?)

Who likes this?

10 people have marked this snippet as a favorite

mattkenefick
vali29
kernelpanics
silentbob84
Juntalis
badcircle
prit
ApeWare
ringo380
zgabievi


PHP Class: Paging


 / Published in: PHP
 

Paging class. Details on use to come later.

  1. <?php
  2. /**************************************
  3.  seesaw associates | http://seesaw.net
  4.  
  5.  client: general
  6.  file: class.paging.php
  7.  description: handles mysql paging
  8.  
  9.  Copyright (C) 2008 Matt Kenefick(.com)
  10. **************************************/
  11.  
  12.  
  13. class paging{
  14.  
  15. var $sql_results;
  16. var $sql_query;
  17.  
  18. var $display_offset;
  19. var $display_limit;
  20. var $display_limit_URI;
  21. var $display_currentpage;
  22. var $display_totalpages;
  23. var $alt_content_count;
  24.  
  25. var $display_offset_var;
  26. var $display_limit_var;
  27.  
  28. var $display_mid_offset;
  29. var $display_link_first;
  30. var $display_link_prev;
  31. var $display_link_next;
  32. var $display_link_last;
  33.  
  34. var $page_filename;
  35. var $page_uri;
  36.  
  37. var $content;
  38. var $content_type;
  39. var $content_count;
  40.  
  41. ## CONSTRUCTOR
  42. function paging($offsetVar='offset',$limit=10,$midOffset=2){
  43. $this->display_offset = isset($_REQUEST[$offsetVar])?$_REQUEST[$offsetVar]:0;
  44. $this->display_limit = is_numeric($limit)?$limit:10;
  45. $this->display_mid_offset = $midOffset;
  46. $this->display_offset_var = $offsetVar;
  47. $this->display_currentpage = ceil($this->display_offset / $this->display_limit);
  48. }
  49.  
  50. ## HANDLE DATA
  51. function setContent($content){
  52. if( is_array($content) )
  53. $this->content_type = 'array';
  54.  
  55. $this->content = $content;
  56. $this->private_contentDetails();
  57. $this->getLinks();
  58. }
  59.  
  60. function private_contentDetails(){
  61. if( $this->content_type == 'array' )
  62. $this->content_count = count($this->content);
  63. }
  64.  
  65. function getContent(){
  66. $returnAry = array();
  67. for(
  68. $i=$this->display_offset;
  69. $i< min( $this->content_count, $this->display_offset+$this->display_limit );
  70. ++$i
  71. )
  72. array_push($returnAry,$this->content[$i]);
  73.  
  74. return $returnAry;
  75. }
  76.  
  77. ## LINKS TO NAVIGATE
  78. function getLinks(){
  79. $this->getNextLink('');
  80. $this->getFirstLink('');
  81. $this->getLastLink('');
  82. $this->getPrevLink('');
  83. }
  84.  
  85. function getNext(){
  86. return $this->display_link_next;
  87. }
  88.  
  89. function getPrev(){
  90. return $this->display_link_prev;
  91. }
  92.  
  93. function getFirst(){
  94. return $this->display_link_first;
  95. }
  96.  
  97. function getLast(){
  98. return $this->display_link_last;
  99. }
  100.  
  101. function getNextLink($caption){
  102. // Check if we're counting content or the alternate user provided count
  103. if( $this->alt_content_count )
  104. $count = $this->alt_content_count;
  105. else
  106. $count = $this->content_count;
  107.  
  108. if( $this->display_offset+$this->display_limit-$count >=0 ){
  109.  
  110. $this->display_link_next = $this->display_offset;
  111. }else{
  112. $this->display_link_next = min(
  113. $count-1,
  114. $this->display_offset+$this->display_limit
  115. );
  116. }
  117. return $this->buildLink( $this->buildNewURI( $this->display_link_next), $caption );
  118. }
  119.  
  120. function getPrevLink($caption){
  121. $this->display_link_prev = max(
  122. 0,
  123. $this->display_offset-$this->display_limit
  124. );
  125. return $this->buildLink( $this->buildNewURI( $this->display_link_prev), $caption );
  126. }
  127.  
  128. function getFirstLink($caption){
  129. $this->display_link_first = 0;
  130. return $this->buildLink( $this->buildNewURI( $this->display_link_first), $caption );
  131. }
  132.  
  133. function getLastLink($caption){
  134. $this->display_link_last = $this->content_count-$this->display_limit;
  135. return $this->buildLink( $this->buildNewURI( $this->display_link_last), $caption );
  136. }
  137.  
  138. ## NUMBERS
  139. function getPageCount(){
  140. if( $this->alt_content_count )
  141. $count = $this->alt_content_count;
  142. else
  143. $count = $this->content_count;
  144.  
  145. $this->display_totalpages = ceil(
  146. $count / $this->display_limit
  147. );
  148. return $this->display_totalpages;
  149. }
  150.  
  151. function getPageNumbers(){
  152.  
  153. // define variables
  154. $midOffset = $this->display_mid_offset;
  155. $firstLink = $this->display_link_first;
  156. $pageCount = $this->getPageCount();
  157. $thisPage = $this->display_currentpage;
  158. $limit = $this->display_limit;
  159. $displayed = $midOffset*2+1;
  160. $returnAry = array();
  161.  
  162. // dots
  163. $returnAry['afterdot'] = '';
  164. $returnAry['beforedot'] = '';
  165.  
  166.  
  167. // if two times and center the number is less than all the pages together
  168. if( ($midOffset*2+1) < $pageCount ){
  169. $min = max($firstLink,$thisPage-$midOffset);
  170. $max = min($pageCount,$thisPage+$midOffset+1);
  171. $total = $max-$min;
  172.  
  173. // this keeps x amount of pages displayed even if
  174. // you're not in mid cause of page 1 or 2
  175. if($total<$displayed){
  176.  
  177. if($min==0){
  178. $max+=$displayed-$total;
  179. }
  180. if($max==$pageCount){
  181. $min-=$displayed-$total;
  182. }
  183. }
  184.  
  185. }else{
  186. # Just output a set of numbers
  187. $min = 0;
  188. $max = $pageCount;
  189. }
  190.  
  191. // run pages, check for current page and name it
  192. for($i=$min;$i<$max;++$i){
  193. $cp = 'no';
  194. if($thisPage==$i)
  195. $cp = 'yes';
  196.  
  197. if($max!=$pageCount)
  198. $returnAry['afterdot'] = '...';
  199.  
  200. if($min>0)
  201. $returnAry['beforedot'] = '...';
  202.  
  203. array_push($returnAry,
  204. 'currentPage' => $cp,
  205. 'pageNumber' => ($i+1),
  206. 'pageLink' => $this->buildLink( $this->buildNewURI( ($i*$limit) ), ($i+1) )
  207. )
  208. );
  209. }
  210.  
  211. return $returnAry;
  212.  
  213. }
  214.  
  215. function makePageNumbers($format, $pages,$boldCurrent=true,$separator=' '){
  216. $retPages = '';
  217.  
  218. // make actual page numbers
  219. foreach($pages as $key => $value):
  220. if(is_numeric($key))
  221. $retPages .= ('yes'==$value['currentPage'] && $boldCurrent)?'<b>'.$value['pageLink'] .'</b>'.$separator:$value['pageLink'].$separator;
  222. endforeach;
  223.  
  224. $format = str_replace( array('{beforedot}',
  225. '{afterdot}',
  226. '{pages}',
  227. '{separator}'),
  228. array( $pages['beforedot'],
  229. $pages['afterdot'],
  230. $retPages),
  231. $format);
  232. return $format;
  233. }
  234.  
  235. ## CHECKS
  236. function isFirstPage(){
  237. if($this->display_currentpage==0)
  238. return true;
  239. return false;
  240. }
  241. function isLastPage(){
  242. // add one because basis is 0, not 1
  243. if($this->display_currentpage+1==$this->getPageCount())
  244. return true;
  245. return false;
  246. }
  247.  
  248. ## FUNCTIONS
  249. function getPageName(){
  250. $fileName = explode('/',$_SERVER['REQUEST_URI']);
  251. $fileName = $fileName[count($fileName)-1];
  252.  
  253. if(strpos($fileName,'?')>0) {
  254. $fileName = explode('?',$fileName);
  255. $fileName = $fileName[0];
  256. }
  257.  
  258. return $fileName;
  259. }
  260.  
  261. function getCleanURI(){
  262. $URI = $_SERVER['REQUEST_URI'];
  263. if(strpos($URI,'?')>0){
  264. $URI = explode('?',$URI);
  265. $URI = $URI[1];
  266.  
  267. $URI = preg_replace('/\b'.$this->display_offset_var.'\b[=0-9&]{2,20}/','',$URI);
  268. //$URI = preg_replace('/\b'.$this->display_limit_var.'\b[=0-9&]{2,20}/','',$URI);
  269.  
  270. return $URI;
  271. }
  272. return false;
  273. }
  274.  
  275. function buildNewURI($offset){
  276. $newFile = $this->getPageName() . '?' . $this->getCleanURI();
  277.  
  278. $lastChar = substr($newFile,strlen($newFile)-1,1);
  279. if( $lastChar != '&' && $lastChar != '?' )
  280. $newFile.='&';
  281.  
  282. $newFile .= $this->display_offset_var.'='.$offset.'&';
  283. //$newFile .= $this->display_limit_var.'='.$limit.'&';
  284.  
  285. return $newFile;
  286. }
  287.  
  288. function buildLink( $href, $caption, $target=NULL ){
  289. if( $target != NULL )
  290. $target = ' target="'.$target.'"';
  291. return '<a href="'.$href.'"'.$target.'>'.$caption.'</a>';
  292. }
  293.  
  294. function encodeURI($str){
  295. $salt = 'falaful';
  296. $str = strrev($salt.$str);
  297. return base64_encode($str);
  298. }
  299.  
  300. function decodeURI($str){
  301. $salt = 'falaful';
  302. $str = strrev( base64_decode($str) );
  303. $str = substr( $str, strlen($salt), strlen($str) );
  304. return $str;
  305. }
  306.  
  307.  
  308.  
  309. ##############
  310. #
  311. # These functions are for inputting a query for this
  312. # class to execute. The other functions will grab all
  313. # x amount of rows then truncate it. These functions will
  314. # only limit to whatever amount. This improves performance.
  315. # Reason is so you dont grab 1,000,000 rows when you want 3.
  316. #
  317. ##############
  318.  
  319. function runWQuery($db, $statement, $table, $where=''){
  320.  
  321. // get total rows
  322. $db->query( 'SELECT COUNT(*) AS count FROM '. $table . ' ' . $where );
  323. $db->movenext();
  324. $this->alt_content_count = $db->col['count'];
  325.  
  326. // add limit to query
  327. $where .= ' LIMIT ' . $this->display_offset .', '.$this->display_limit;
  328.  
  329. // save query
  330. $this->sql_query = $statement . ' FROM ' . $table .' '. $where;
  331.  
  332. // print query
  333. //echo $this->sql_query;
  334.  
  335. // run query
  336. $db->query( $this->sql_query );
  337. $this->sql_results = array();
  338.  
  339. // save results
  340. while($db->movenext()){
  341. array_push($this->sql_results , $db->col);
  342. }
  343.  
  344. return $this->runQueryActions();
  345. }
  346.  
  347. function runQueryActions(){
  348. $this->setContent( $this->sql_results );
  349.  
  350. $pages = $this->getPageNumbers();
  351.  
  352. // make actual page numbers
  353. $retPages = $this->makePageNumbers( '{beforedot} {pages} {afterdot}', $pages, true, ' ' );
  354.  
  355. // get new display
  356. return array(
  357. 'content' => $this->sql_results,
  358. 'pages' => $retPages
  359. );
  360. }
  361. }
  362.  
  363. ?>

Report this snippet  

You need to login to post a comment.