La classe a besoin de connaître la page en cours pour construire correctement le système de pagination.
Source / Exemple :
<?php
/******************************************************************
Projectname: Panigate Class
Version: 1.0b
Author: Oussi Yassine <darkinux@gmail.com>
Last modified: 25 Avril 2009
Copyright (C): 2009 Boxcreation, All Rights Reserved
class paginate
{
public $output;
public $nbTotal;
private $_getPage;
public $nbPerPage;
public $currentPage;
public $nbPages;
public $firstId;
public $preview;
public $next;
public $first;
public $last;
public function generate($nbTotal, $nbPerPage = 1, &$getPage)
{
$this->nbTotal = (int) $nbTotal;
$this->nbPerPage = (int) $nbPerPage;
$this->nbPages = ceil($this->nbTotal / $this->nbPerPage);
$this->_getPage = (int) $getPage;
unset($this->output);
$this->currentPage = ( !empty($this->_getPage) && ($this->_getPage > 1) &&
($this->_getPage <= $this->nbPages ) ) ? $this->_getPage : 1;
$this->firstId = ( $this->currentPage - 1 ) * $this->nbPerPage;
if ( $this->nbPages > 1 )
{
if($this->currentPage+4 > $this->nbPages)
{
$this->last = $this->nbPages;
}
elseif($this->currentPage-4 > 1)
{
$this->last = $this->currentPage+4;
}
else
{
$this->last = $this->currentPage+4;
}
if($this->currentPage-6 <= 0)
{
$this->first = 1;
}
elseif($this->currentPage+4 < $this->nbPages)
{
$this->first = $this->currentPage-4;
}
else
{
$this->first = $this->currentPage-6;
}
for ( $i=$this->first; $i <= $this->last; $i++ )
{
if( $this->currentPage != $i )
{
$this->output[$i] = 1;
}
else
{
$this->output[$i] = 2;
}
}
if($this->last < $this->nbPages)
{
$this->output[$i] = 3;
}
}
else
{
$this->output = NULL;
}
}
public function nbPages()
{
return $this->nbPages;
}
public function currentPage()
{
return $this->currentPage;
}
public function getPaginate()
{
return $this->output;
}
public function firstId()
{
return $this->firstId;
}
}
?>
Exemple d'utilisation:
// Calculate Result
if($search->search($tag) > $nbPerpage)
{
$paginate = new paginate();
// Paginate
if(isset($_GET["page"]) && !empty($_GET["page"]))
{
$page = $_GET["page"];
}
$nbResult = $search->search($tag);
$oS->assign("total",$nbResult);
$paginate->generate($nbResult, $nbPerpage, $page);
if (is_array($paginate->getPaginate()) )
{
if($paginate->currentPage() > 1 )
{
$oS->assign("preview",$paginate->currentPage()-1);
}
$oS->assign("paginate",$paginate->getPaginate());
if($paginate->currentPage() < $paginate->nbPages())
{
$oS->assign("next",$paginate->currentPage()+1);
}
}
$oS->assign("firstId",$paginate->firstId);
$lastentry = ($paginate->firstId+$nbPerpage)-$nbResult;
$oS->assign("lastentry",($paginate->firstId+$nbPerpage)-$lastentry);
$firstId = $paginate->firstId;
}
Exemple d'utilisateur avec smarty :
<div id="paginate">
{if $preview neq NULL}
<a href="/search/{$preview}/?tag={$tag}">« Preview</a>
{/if}
{foreach from=$paginate key=id item=page}
{if $page eq 1}
<a href="/search/{$id}/?tag={$tag}">{$id}</a>
{elseif $page eq 2}
<span class="selectednow">{$id}</span>
{elseif $page eq 3}
...
{/if}
{/foreach}
{if $next neq NULL}
<a href="/search/{$next}/?tag={$tag}">Next »</a>
{/if}
</div>
Une bref explication :
la method $paginate->getPaginate() renvoi un tableau :
array(6) { [1]=> int(2) [2]=> int(1) [3]=> int(1) [4]=> int(1) [5]=> int(1) [6]=> int(3) }
Et quand une clé [X]=> int(1) >> Les autres Pages;
Et quand une clé [X]=> int(2) >> Page actuel;
Et quand une clé [X]=> int(3) >> Afficher les trois points (...);
Si vous avez des suggestions n'hesité pas.
Conclusion :
J'ai besoin de vos suggestions.
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.