1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
class Pagination
{
public $output; // Sortie HTML;
public $nbtotal; // Nombre total de liens, de news, de n'importe quoi :)
public $_getName; // Nom du _GET pour l'affichage des pages !
public $nbmaxparpage; // Nombre d'affichage par page
private $nbdepages; // Nombre de pages nécessaires
public $minid; // Retourne l'ID du premier enregistrement pour la page en cours
public function __construct( $nbtotal, $nbmaxparpage = 10, $getName = 'page')
{
$this->nbtotal = (int) $nbtotal;
$this->nbmaxparpage = (int) $nbmaxparpage;
$this->nbdepages = ceil($this->nbtotal / $this->nbmaxparpage);
$this->_getName = $getName;
}
public function Generate()
{
unset($this->output);
$pageencours = ( isset($_GET[$this->_getName]) && (int) $_GET[$this->_getName] > 1 ) ? (int) $_GET[$this->_getName] : 1;
$this->minid = ( $pageencours - 1 ) * $this->nbmaxparpage;
if ( $this->nbdepages > 1 )
{
for ( $i=1; $i <= $this->nbdepages; $i++ )
{
if ( $i === $pageencours )
{
$this->output[] = array('link' => FALSE, 'page' => $i);
} else
{
$this->output[] = array('link' => TRUE, 'page' => $i);
}
}
}
else
{
$this->output = NULL;
}
}
}
<?php
//L'utilisation ?
$query = mysql_query('SELECT COUNT(*) FROM news');
$data = mysql_fetch_row($query); // Supposons que vous récupérer le nombre max de news
$num_rows = (int) $data[0];
$Pagination = new Pagination ( $num_rows, 10 ); // Affichera 10 news par page
$Pagination->Generate; // Génére la pagination (peut y avoir d'autres options à changer avant, c'est vous qui voyez !)
if ( isset($Pagination->output) && is_array($Pagination->output) )
{ // On vérifie que y'a bien un output, sinon on zap !
$var = '<span> Page : ';
foreach ( $Pagination->output as $key )
{ // On parcours le tableau
if ( $key['link'] )
{
$var .= '<a href="./news.php'.$Pagination->_getName.'='.$key['page'].'">'.$key['page'].'</a> ';
}
else
{
$var .= $key['page'].' '; // Si on est sur la bonne page, on met pas de lien.
}
}
$var .= '</span>';
}
if ( isset($var) ) echo $var; // On affiche le tout.
?> |
Partager