Kolejna klasa w moim wykonaniu. Proszę o ocenę oraz sugestie... wszelkie komentarze bardzo mile widziane. Jako, że klasy nie testowałem jeszcze dogłębnie nie jest pewnie wolna od błędów.
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 -> count_from
= ( !empty( $_GET['p'] ) ?
$_GET['p'] : 0
);
$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 = ( $this -> url == '?' ) ? '' : $this -> url;
return true;
}
private function pageNavigator()
{
$pages = ceil( $this -> count_all
/ $this -> per_page
);
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 .= '*';
}
}
$pageNavigator = ereg_replace( '[*]+', '...', $pageNavigator );
return $pageNavigator;
}
else
{
return false;
}
}
private function counterNavigator()
{
$pages = ceil( $this -> count_all
/ $this -> per_page
);
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ę
$sql = mysql_query( 'SELECT * FROM tabela LIMIT '. $nav -> count_from
.','. $nav -> per_page
);
{
// 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
?>
Ten post edytował CzesLaW'ek 29.05.2005, 22:42:40