- /**************************************
- File: class. paging. php
- Php paging code
- **************************************/
- 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_offset = isset ($ _ REQUEST [$ offsetVar])? $ _ REQUEST [$ offsetVar]: 0;
- $ This-> display_limit = is_numeric ($ limit )? $ Limit: 10;
- $ This-> display_mid_offset = $ midOffset;
- $ This-> display_offset_var = $ offsetVar;
- $ This-> display_currentpage = ceil ($ this-> display_offset/$ this-> display_limit );
- }
- # HANDLE DATA
- Function setContent ($ content ){
- If (is_array ($ content ))
- $ This-> content_type = 'array ';
- $ This-> content = $ content;
- $ This-> private_contentDetails ();
- $ This-> getLinks ();
- }
- Function private_contentDetails (){
- If ($ this-> content_type = 'array ')
- $ This-> content_count = count ($ this-> content );
- }
- Function getContent (){
- $ ReturnAry = array ();
- For (
- $ I = $ this-> display_offset;
- $ I <min ($ this-> content_count, $ this-> display_offset + $ this-> display_limit );
- ++ $ I
- )
- Array_push ($ returnAry, $ this-> content [$ 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 {
- $ This-> display_link_next = min (
- $ Count-1,
- $ This-> display_offset + $ this-> display_limit
- );
- }
- Return $ this-> buildLink ($ this-> buildNewURI ($ this-> display_link_next), $ caption );
- }
- Function getPrevLink ($ caption ){
- $ This-> display_link_prev = max (
- 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;
- $ This-> display_totalpages = ceil (
- $ 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;
- $ ReturnAry = array ();
- // Dots
- $ ReturnAry ['afterdot '] = '';
- $ ReturnAry ['beforedot '] = '';
- // If two times and center the number is less than all the pages together
- If ($ midOffset * 2 + 1) <$ pageCount ){
- $ Min = max ($ firstLink, $ thisPage-$ midOffset );
- $ Max = min ($ pageCount, $ thisPage + $ midOffset + 1 );
- $ 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 ['foreud'] = '...';
- Array_push ($ returnAry,
- Array (
- '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 ):
- If (is_numeric ($ key ))
- $ RetPages. = ('yes' = $ value ['currentpage'] & $ boldCurrent )? ''. $ Value ['pagelink'].''. $ Separator: $ value ['pagelink']. $ separator;
- Endforeach;
- $ Format = str_replace (array ('{beforedot }',
- '{Afterdot }',
- '{Pages }',
- '{Separator }'),
- Array ($ pages ['beforedot '],
- $ 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 = explode ('/', $ _ SERVER ['request _ URI ']);
- $ FileName = $ fileName [count ($ fileName)-1];
- If (strpos ($ fileName ,'? ')> 0 ){
- $ FileName = explode ('? ', $ FileName );
- $ FileName = $ fileName [0];
- }
- Return $ fileName;
- }
- Function getCleanURI (){
- $ URI = $ _ SERVER ['request _ URI '];
- If (strpos ($ URI ,'? ')> 0 ){
- $ URI = explode ('? ', $ URI );
- $ URI = $ URI [1];
- $ URI = preg_replace ('/\ B'. $ this-> display_offset_var. '\ B [= 0-9 &] {2, 20}/', '', $ URI );
- // $ 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 ();
- $ LastChar = substr ($ newFile, strlen ($ newFile)-1, 1 );
- 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 ''. $ caption .'';
- }
- Function encodeURI ($ str ){
- $ Salt = 'falaful ';
- $ Str = strrev ($ salt. $ str );
- Return base64_encode ($ str );
- }
- Function decodeURI ($ str ){
- $ Salt = 'falaful ';
- $ Str = strrev (base64_decode ($ str ));
- $ Str = substr ($ str, strlen ($ salt), strlen ($ str ));
- 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 );
- $ This-> SQL _results = array ();
- // Save results
- While ($ db-> movenext ()){
- Array_push ($ this-> SQL _results, $ db-> col );
- }
- 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
- Return array (
- 'Content' => $ this-> SQL _results,
- 'Pages '=> $ retPages
- );
- }
- }
- ?>
|