/ Published in: PHP
Paging class. Details on use to come later.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /************************************** seesaw associates | http://seesaw.net client: general file: class.paging.php description: handles mysql paging Copyright (C) 2008 Matt Kenefick(.com) **************************************/ class paging{ var $sql_results; var $sql_query; var $display_offset; var $display_limit; var $display_limit_URI; var $display_currentpage; var $display_totalpages; var $alt_content_count; var $display_offset_var; var $display_limit_var; var $display_mid_offset; var $display_link_first; var $display_link_prev; var $display_link_next; var $display_link_last; var $page_filename; var $page_uri; var $content; var $content_type; var $content_count; ## CONSTRUCTOR function paging($offsetVar='offset',$limit=10,$midOffset=2){ $this->display_mid_offset = $midOffset; $this->display_offset_var = $offsetVar; } ## HANDLE DATA function setContent($content){ $this->content_type = 'array'; $this->content = $content; $this->private_contentDetails(); $this->getLinks(); } function private_contentDetails(){ if( $this->content_type == 'array' ) } function getContent(){ for( $i=$this->display_offset; ++$i ) return $returnAry; } ## LINKS TO NAVIGATE function getLinks(){ $this->getNextLink(''); $this->getFirstLink(''); $this->getLastLink(''); $this->getPrevLink(''); } function getNext(){ return $this->display_link_next; } function getPrev(){ return $this->display_link_prev; } function getFirst(){ return $this->display_link_first; } function getLast(){ return $this->display_link_last; } function getNextLink($caption){ // Check if we're counting content or the alternate user provided count if( $this->alt_content_count ) $count = $this->alt_content_count; else $count = $this->content_count; if( $this->display_offset+$this->display_limit-$count >=0 ){ $this->display_link_next = $this->display_offset; }else{ $count-1, $this->display_offset+$this->display_limit ); } return $this->buildLink( $this->buildNewURI( $this->display_link_next), $caption ); } function getPrevLink($caption){ 0, $this->display_offset-$this->display_limit ); return $this->buildLink( $this->buildNewURI( $this->display_link_prev), $caption ); } function getFirstLink($caption){ $this->display_link_first = 0; return $this->buildLink( $this->buildNewURI( $this->display_link_first), $caption ); } function getLastLink($caption){ $this->display_link_last = $this->content_count-$this->display_limit; return $this->buildLink( $this->buildNewURI( $this->display_link_last), $caption ); } ## NUMBERS function getPageCount(){ if( $this->alt_content_count ) $count = $this->alt_content_count; else $count = $this->content_count; $count / $this->display_limit ); return $this->display_totalpages; } function getPageNumbers(){ // define variables $midOffset = $this->display_mid_offset; $firstLink = $this->display_link_first; $pageCount = $this->getPageCount(); $thisPage = $this->display_currentpage; $limit = $this->display_limit; $displayed = $midOffset*2+1; // dots $returnAry['afterdot'] = ''; $returnAry['beforedot'] = ''; // if two times and center the number is less than all the pages together if( ($midOffset*2+1) < $pageCount ){ $total = $max-$min; // this keeps x amount of pages displayed even if // you're not in mid cause of page 1 or 2 if($total<$displayed){ if($min==0){ $max+=$displayed-$total; } if($max==$pageCount){ $min-=$displayed-$total; } } }else{ # Just output a set of numbers $min = 0; $max = $pageCount; } // run pages, check for current page and name it for($i=$min;$i<$max;++$i){ $cp = 'no'; if($thisPage==$i) $cp = 'yes'; if($max!=$pageCount) $returnAry['afterdot'] = '...'; if($min>0) $returnAry['beforedot'] = '...'; 'currentPage' => $cp, 'pageNumber' => ($i+1), 'pageLink' => $this->buildLink( $this->buildNewURI( ($i*$limit) ), ($i+1) ) ) ); } return $returnAry; } function makePageNumbers($format, $pages,$boldCurrent=true,$separator=' '){ $retPages = ''; // make actual page numbers foreach($pages as $key => $value): $retPages .= ('yes'==$value['currentPage'] && $boldCurrent)?'<b>'.$value['pageLink'] .'</b>'.$separator:$value['pageLink'].$separator; endforeach; '{afterdot}', '{pages}', '{separator}'), $pages['afterdot'], $retPages), $format); return $format; } ## CHECKS function isFirstPage(){ if($this->display_currentpage==0) return true; return false; } function isLastPage(){ // add one because basis is 0, not 1 if($this->display_currentpage+1==$this->getPageCount()) return true; return false; } ## FUNCTIONS function getPageName(){ $fileName = $fileName[0]; } return $fileName; } function getCleanURI(){ $URI = $_SERVER['REQUEST_URI']; $URI = $URI[1]; //$URI = preg_replace('/\b'.$this->display_limit_var.'\b[=0-9&]{2,20}/','',$URI); return $URI; } return false; } function buildNewURI($offset){ $newFile = $this->getPageName() . '?' . $this->getCleanURI(); if( $lastChar != '&' && $lastChar != '?' ) $newFile.='&'; $newFile .= $this->display_offset_var.'='.$offset.'&'; //$newFile .= $this->display_limit_var.'='.$limit.'&'; return $newFile; } function buildLink( $href, $caption, $target=NULL ){ if( $target != NULL ) $target = ' target="'.$target.'"'; return '<a href="'.$href.'"'.$target.'>'.$caption.'</a>'; } function encodeURI($str){ $salt = 'falaful'; } function decodeURI($str){ $salt = 'falaful'; return $str; } ############## # # These functions are for inputting a query for this # class to execute. The other functions will grab all # x amount of rows then truncate it. These functions will # only limit to whatever amount. This improves performance. # Reason is so you dont grab 1,000,000 rows when you want 3. # ############## function runWQuery($db, $statement, $table, $where=''){ // get total rows $db->query( 'SELECT COUNT(*) AS count FROM '. $table . ' ' . $where ); $db->movenext(); $this->alt_content_count = $db->col['count']; // add limit to query $where .= ' LIMIT ' . $this->display_offset .', '.$this->display_limit; // save query $this->sql_query = $statement . ' FROM ' . $table .' '. $where; // print query //echo $this->sql_query; // run query $db->query( $this->sql_query ); // save results while($db->movenext()){ } return $this->runQueryActions(); } function runQueryActions(){ $this->setContent( $this->sql_results ); $pages = $this->getPageNumbers(); // make actual page numbers $retPages = $this->makePageNumbers( '{beforedot} {pages} {afterdot}', $pages, true, ' ' ); // get new display 'content' => $this->sql_results, 'pages' => $retPages ); } } ?>