Сам класс:
pagination.php (Download)
<?php
namespace Oddler\SOLib;
class pagination
{
protected $_iLimitStart = 0;
protected $_iLimit = 0;
protected $_iStartShowing = 0;
protected $_iEndShowing = 0;
protected $_iTotalPages = 0;
protected $_iActivePage = 0;
/**
* Pagination constructor.
* @param int $iTotalItems
* @param int $iItemsOnPage
* @param int $iShowPages
* @param int $iActivePage
*/
function __construct(int $iTotalItems, int $iItemsOnPage, int $iShowPages, int $iActivePage = 1) {
$iTotalPages = floor($iTotalItems / $iItemsOnPage);
$this->_iLimitStart = 1;
$this->_iLimit = $iItemsOnPage;
$this->_iActivePage = $iActivePage;
if ($iTotalPages > 1) {
$iLeft = $iActivePage - 1;
if ($iLeft < floor($iShowPages / 2)) {
$iStart = 1;
}
else {
$iStart = $iActivePage - floor($iShowPages / 2);
}
$iEnd = $iStart + $iShowPages - 1;
if ($iEnd > $iTotalPages){
$iStart -= ($iEnd - $iTotalPages);
$iEnd = $iTotalPages;
if ($iStart < 1) {
$iStart = 1;
}
}
$this->_iTotalPages = $iTotalPages;
$this->_iStartShowing = $iStart;
$this->_iEndShowing = $iEnd;
$this->_iLimitStart = $iActivePage * $iItemsOnPage;
}
}
/**
* Getter for LimitStart
* @return int
*/
public function getLimitStart(){
return $this->_iLimitStart;
}
/**
* Getter for Limit
* @return int
*/
public function getLimit(){
return $this->_iLimit;
}
public function displayNavbar($sBaseUrl){
$sPageUrl = $sBaseUrl.'?page=';
$iActivePage = $this->_iActivePage;
$iStart = $this->_iStartShowing;
$iEnd = $this->_iEndShowing;
$iTotalPages = $this->_iTotalPages;
$sRet = '<div id="soPagination"><span>Страницы: </span>';
if ($iActivePage != 1) {
$sUrl = $iActivePage == 2?$sBaseUrl:$sPageUrl.($iActivePage - 1);
$sRet .= '<a href="'.$sBaseUrl.'" title="Первая страница"><<<</a>';
$sRet .= '<a href="'.$sUrl.'" title="Предыдущая страница"><</a>';
}
for ($i = $iStart; $i <= $iEnd; $i++) {
if($i == $iActivePage){
$sRet .= '<span>'.$i.'</span>';
}
else {
$sUrl = $i == 1?$sBaseUrl:$sPageUrl.$i;
$sRet .= '<a href="'.$sUrl.'">'.$i.'</a>';
}
}
if ($iActivePage != $iTotalPages){
$sRet .= '<a href="'.$sPageUrl.($iActivePage + 1).'" title="Следующая страница">></a>';
$sRet .= '<a href="'.$sPageUrl.$iTotalPages.'" title="Последняя страница">>>></a>';
}
$sRet .= '</div>';
return $sRet;
}
}
Пример использования:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
require_once('vendor/autoload.php');
require_once('pagination.php');
use Oddler\Pdo\DBCore;
use Oddler\SOLib\pagination;
$DBCore = DBCore::getInstance();
$oDB1 = $DBCore->connect( array(
'host' => 'localhost',
'user' => 'USER',
'password' => 'PASS',
'database' => 'db_tests',
'charset' => 'utf8',
'database_type' => 'mysql'
));
$iActivePage = isset($_REQUEST['page'])?$_REQUEST['page']:10;
$iItemsOnPage = 7;
$iShowPages = 10;
$oDB1->setQuery('SELECT count(*) AS c FROM `tbl_pagination`');
$iTotalItems = $oDB1->loadObject()->c;
$oPagination = new pagination($iTotalItems, $iItemsOnPage, $iShowPages, $iActivePage);
$oDB1->setQuery('SELECT * FROM `tbl_pagination` LIMIT '.$oPagination->getLimitStart().', '.$oPagination->getLimit());
$aRows = $oDB1->loadObjectsList();
foreach ($aRows as $oRow) {
echo '<b>'.$oRow->title.'</b>: '.$oRow->text.'<br />';
}
echo $oPagination->displayNavbar('/index.php');