Non avendo nulla fare
Codice PHP:
class Pagination
{
protected
$currentPage = null,
$maxItems = 10,
$data = array(),
$totalPages = null;
public function setData(array $data)
{
$this->data = $data;
return $this;
}
public function getData()
{
return $this->data;
}
public function setMaxItems($value)
{
$this->maxItems = $value;
return $this;
}
public function getMaxItems()
{
return $this->maxItems;
}
public function setCurrentPage($value)
{
if ($value > $total = $this->getTotalPages())
{
$value = $total;
}
$this->currentPage = $value;
return $this;
}
public function getCurrentPage()
{
return $this->currentPage;
}
public function getTotalPages()
{
if (!$this->totalPages)
{
if (!$data = $this->getData())
{
$this->totalPages = 1;
}
else
{
$this->totalPages = ceil(count($data)/$this->getMaxItems());
}
}
return $this->totalPages;
}
public function getPrevPage()
{
return $this->getCurrentPage() == 1 ? null : $this->getCurrentPage() - 1;
}
public function getNextPage()
{
return $this->getCurrentPage() >= $this->getTotalPages() ? null : $this->getCurrentPage() + 1;
}
public function execute()
{
return array_slice($this->getData(), ($this->getMaxItems() * $this->getCurrentPage()) - $this->getMaxItems(), $this->getMaxItems());
}
}
class PaginationWrapper extends Pagination
{
public function __construct($pattern, $maxItems)
{
$this->setData(glob($pattern));
$this->setMaxItems($maxItems);
}
public function getCurrentPage()
{
if (!$this->currentPage)
{
$page = 1;
if (isset($_GET['page']))
{
$page = $_GET['page'] <= 0 || !is_numeric($_GET['page']) ? 1 : $_GET['page'];
}
$this->setCurrentPage($page);
}
return parent::getCurrentPage();
}
public function getPagination()
{
if (!$result = $this->execute())
{
$out = null;
}
else
{
$out = '<ul>';
foreach ($this->execute() as $item)
{
$out .= '[*][url="' . str_replace('preview', 'main', $item) . '"][img]' . $item. '[/img][/url]';
}
$out .= '[/list]';
}
return $out;
}
public function getNavigation()
{
$out = '<ul>';
$out .= $this->getPrevPage() ? '<li class="prev">[url="?page=' . $this->getPrevPage() . '"]back[/url]' : '';
$out .= $this->getNextPage() ? '<li class="next">[url="?page=' . $this->getNextPage() . '"]next[/url]' : '';
$out .= '[/list]';
return $out;
}
public function getAllAsString()
{
if (!$this->getPagination())
{
return 'Nessun dato da impaginare';
}
return $out = $this->getPagination() . $this->getNavigation();
}
}
$pagination = new PaginationWrapper('*.jpg', 16);
echo $pagination->getAllAsString();
altrimenti se vuoi usare la soluzione precedente
Codice PHP:
if (!$images = glob('*.jpg'))
{
$out = 'Nessun dato da impaginare';
}
else
{
$nImages = count($images);
$xPagina = 16;
$nPagine = ceil($nImages/$xPagina);
$page = 1;
if (isset($_GET['page']))
{
$page = $_GET['page'] <= 0 || !is_numeric($_GET['page']) ? 1 : $_GET['page'];
$page = $nPagine < $page ? $nPagine : $page;
}
$out = '<ul>';
foreach (array_slice($images, ($xPagina * $page) - $xPagina, $xPagina) as $image)
{
$out .= '[*][url="' . str_replace('preview', 'main', $image) . '"][img]' .$image . '[/img][/url]';
}
$out.= '[/list]';
$out .= '<ul id="prevnext">';
$out .= $page == 1 ? '' : '<li class="prev">[url="' . $_SERVER['PHP_SELF'] .'?page=' . ($page - 1) . '"]INDIETRO[/url]';
$out .= $page >= $nPagine ? '' : '<li class="next">[url="' . $_SERVER['PHP_SELF'] .'?page=' . ($page + 1) . '"]AVANTI[/url]';
$out .= '[/list]';
}
echo $out;
Usa quella che preferisci e ovviamente modificale e migliorale come ritieni più opportuno