Przeznaczenie
- Porcjowanie wyników na stronie
- Wyświetlanie nawigatora w dwóch różnych formatach
<?php ######################################## ## ## myPaginator v0.9 b ## Written by CzesLaW ## ## E-Mail: czesio@e-clubbing.net ## Website: http://e-clubbing.net ## ######################################## ## ## Jeśli masz jakieś sugestie odnośnie skryptu, ## napisz do mnie e-mail'a. ## ######################################## class myPaginator { public $navigator = ''; public $url = ''; public $count_from = 0; public $per_page = 0; public $count_all = 0; public function myPaginator( $per_page = 5 ) { $this -> per_page = $per_page; return true; } public function showNavigator( $count_all = 0, $type = 0 , $long = 1) { if( $count_all !== 0 ) { $this -> count_all = $count_all; $this -> getUrl(); if( $this -> count_from > 0 ) { $this -> navigator .= '<a href=\"./'. $this -> url . ( ( !empty( $this -> url ) ) ? '&' : '?' ) .'p='. ( $this -> count_from - $this -> per_page ) .'\"><<'; $this -> navigator .= ( $long == 1 ) ? ' <strong>Poprzednie</strong>' : ''; $this -> navigator .= '</a> '; } $this -> navigator .= ( $type == 0 ) ? $this -> pageNavigator() : $this -> counterNavigator(); if( $this -> count_all > ( $this -> count_from + $this -> per_page ) ) { $this -> navigator .= '<a href=\"./'. $this -> url . ( ( !empty( $this -> url ) ) ? '&' : '?' ) .'p='. ( $this -> count_from + $this -> per_page ) .'\">'; $this -> navigator .= ( $long == 1 ) ? '<strong>Następne</strong> ' : ''; $this -> navigator .= '>></a>'; } return $this -> navigator; } else { return false; } } private function getUrl() { $this -> url = substr( str_replace( '&', '&', preg_replace( '!(.*?)[&|?]p=.*!si', '$1', $_SERVER['REQUEST_URI'] ) ), 1 ); $this -> url = ( $this -> url == '?' ) ? '' : $this -> url; return true; } private function pageNavigator() { if( $pages > 1 ) { $pageNavigator = ''; for( $x = 0; $x < $pages; $x++ ) { if( $x < 3 || $x >= ( $pages - 3 ) || ( $x >= ( ( $this -> count_from / $this -> per_page ) - 1 ) && $x <= ( ( $this -> count_from / $this -> per_page ) + 1 ) ) ) { if( $x == ( $this -> count_from / $this -> per_page ) ) { $pageNavigator .= '[<strong>'. ( $x + 1 ) .'</strong>] '; } else { $pageNavigator .= '<a href=\"./'. $this -> url . ( ( !empty( $this -> url ) ) ? '&' : '?' ) .'p='. ( $x * $this -> per_page ) .'\">'. ( $x + 1) .'</a> '; } } else { $pageNavigator .= '*'; } } return $pageNavigator; } else { return false; } } private function counterNavigator() { if( $pages > 1 ) { $pageNavigator = '<strong>'. ( ( $this -> count_from / $this -> per_page ) + 1 ) .'</strong> '; $pageNavigator .= '/'; $pageNavigator .= ' <strong>'. $pages .'</strong> '; return $pageNavigator; } else { return false; } } } ?>
Przykład zastosowania:
- Dzielenie wyników przy mySQL'u:
<?php require_once ( './myPaginator.class.php' ); $nav = new myPaginator( 8 ); // w nawiasie podajemy ile rekordów na stronę { // itd... } echo $nav -> showNavigator( mysql_num_rows( $sql ), 1, 0 ); // w nawiasie po kolei: liczba wszystkich rekordów, tryb stron / licznika, format skrócony / pełny ?>