ADD первый вариант пагинации

This commit is contained in:
Krivchikov Dmitry 2015-10-27 00:25:44 +03:00
parent 95d7c303d9
commit 8a8408302e
3 changed files with 54 additions and 4 deletions

View File

@ -147,7 +147,13 @@ abstract class Model
$sql = 'SELECT * FROM `' . $this->_tableName_ . '`';
$sql .= isset($params['where']) && $params['where'] ? ' WHERE ' . $params['where'] : '';
$sql .= isset($params['order']) && $params['order'] ? ' ORDER BY ' . $params['order'] : '';
$sql .= isset($params['limit']) && $params['limit'] ? ' LIMIT ' . $params['limit'] : '';
if (isset($params['limit']) && $params['limit']) {
if (isset($params['offset']) && $params['offset']) {
$sql .= ' LIMIT ' . $params['offset'] . ', ' . $params['limit'];
} else {
$sql .= ' LIMIT ' . $params['limit'];
}
}
$res = App::DB()->query($sql);
$ready = array();
@ -298,4 +304,18 @@ abstract class Model
$this->_vals_[$this->_primaryKey_] = $this->_id_ = NULL;
}
function getCount($params = array())
{
$sql = 'SELECT count(*) AS `cnt` FROM `' . $this->_tableName_ . '`';
$sql .= isset($params['where']) && $params['where'] ? ' WHERE ' . $params['where'] : '';
$res = App::DB()->query($sql);
$ready = array();
$row = $res->fetch(PDO::FETCH_ASSOC);
return (int)$row['cnt'];
}
}

View File

@ -2,6 +2,7 @@
class PageController extends Controller
{
const elementsByPage = 50;
static function actionPoints()
{
@ -37,6 +38,9 @@ class PageController extends Controller
}
// ------------------------------------------
$pointsCount = Point::model()->getCount($param);
$pagesCount = ceil($pointsCount/self::elementsByPage);
$page = isset($_GET['page']) ? abs((int)$_GET['page']) : $pagesCount;
$param = array();
$param['order'] = 'id DESC';
if (App::getParam('category')) {
@ -46,9 +50,13 @@ class PageController extends Controller
} else {
$param['where'] = '`moderate` = 1';
if (!(App::$user && App::$user->isAdmin)) {
$param['limit'] = 100;
$param['limit'] = self::elementsByPage;
if($page) {
$param['offset'] = self::elementsByPage * ($pagesCount - $page);
}
}
}
$points = Point::model()->getAll($param);
$categories = Category::model()->getAll(array('order' => 'ord ASC'));
@ -69,8 +77,10 @@ class PageController extends Controller
self::render('pointsTemplate.php', array(
'categories' => $categories,
// 'user' => App::$user,
'points' => $result,
'pointsCount' => $pointsCount,
'page' => $page,
'pagesCount' => $pagesCount,
'notModeratedPoints' => $notModeratedPoints,
'randomPoint' => Point::model()->getRandom(),
));

View File

@ -39,8 +39,9 @@ if (App::getParam('user')) {
print "</a>";
}
?>
<a href='/page/points/'>Все точки</a>
<?php
$header = 'Неполный список точек (100 недавно добавленных)';
$header = 'Список точек';
if ((int) App::getParam('category')) {
$header = 'Список точек в категории "' . $categories[(int) App::getParam('category')]->name . '"';
}
@ -58,6 +59,25 @@ if (App::getParam('user')) {
</div>
</div>
</div>
<?php if (!(int) App::getParam('category')):?>
<nav>
<ul class="pagination">
<li class="<?= ($page >= $pagesCount) ? 'disabled' : '' ?>">
<a href="<?= ($page >= $pagesCount) ? '#' : '?page='.($page+1) ?>" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php for ($index = $pagesCount; $index > 0; $index--): ?>
<li><a href="?page=<?= $index ?>" style="<?= $index==$page ? 'background-color: #eeeeed;' : '' ?>"><?= $index ?></a></li>
<?php endfor; ?>
<li class="<?= ($page <= 1) ? 'disabled' : '' ?>">
<a href="<?= ($page <= 1) ? '#' : '?page='.($page-1) ?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
<?php endif; ?>
<hr/>
<div class="row">
<div class="col-md-12">