se mi e' possibile vorrei aggiungere un metodo facile facile per fare una semplice impaginazione con i link first, prev, next e last, in questo esempio con filtro pagina per evitare di clickare inutilmente, ad esempio, last quando siamo gia' a last, o next, etc. etc.
codice:
<?php
class SimplePaginator {
var $first = 0;
var $last = 0;
var $prev = 0;
var $next = 0;
var $limit = 0;
function SimplePaginator( &$limit, &$page, &$rows ) {
$limit = (int) $limit;
$page = (int) $page;
$rows = (int) $rows;
$this->last = &ceil( $rows / $limit );
if( $page > $this->last || $page < 1 ) {
$page = 1;
}
if( $page < $this->last ) {
$this->next = $page + 1;
}
if( $page > 1 ) {
$this->prev = $page - 1;
$this->first = 1;
}
$this->limit = ( $page - 1 ) * $limit;
}
}
// QUAMNTI RECORDS ALLA VOLTA VOGLIO MOSTRARE ?
$daMostrare = 5;
// IN QUALE PAGINA MI TROVO ?
$pagina = isSet( $_GET['page'] ) == true ? (int)$_GET['page'] : 1;
// QUANTI RECORDS IN TOTALE HO NEL DATABASE ?
// ( in questo caso non passo il risultato da db ma lo imposto a mano )
$totaleInDatabase = 13;
// OGGETTO PAGINA
$page = &new SimplePaginator( $daMostrare, $pagina, $totaleInDatabase );
// ESEMPIO IN HTML
// ( controlo che i valori siano diversi da 0 e dalla pagina corrente )
$output = '';
if( $pagina != $page->first && $page->first !== 0 ) {
$output .= '<< &nbsp; ';
}
else {
$output .= '<< &nbsp; ';
}
if( $pagina != $page->prev && $page->prev !== 0 ) {
$output .= '< &nbsp; ';
}
else {
$output .= '< &nbsp; ';
}
$output .= ''.$pagina.' / '.$page->last.' ';
if( $pagina != $page->next && $page->next !== 0 ) {
$output .= '> &nbsp; ';
}
else {
$output .= '> &nbsp; ';
}
if( $pagina != $page->last && $page->last !== 0 ) {
$output .= '>> &nbsp; ';
}
else {
$output .= '>> &nbsp; ';
}
echo $output;
// ESEMPIO QUERY
echo '<hr />SELECT * FROM tabella LIMIT '.$page->limit.', '.$daMostrare.'
';
// ESEMPIO RISULTATI
$row = 0;
$start = $page->limit;
while( $start < $totaleInDatabase && $row < $daMostrare ) {
echo 'risultato '.$row.' della query
';
$start++;
$row++;
}
?>
magari torna utile a qualcuno