Optimize selection of polls in admin page

This commit is contained in:
Olivier PEREZ 2015-09-13 00:53:03 +02:00
parent 7babf3f3a3
commit 24a7fb7df2
3 changed files with 34 additions and 8 deletions

View File

@ -65,9 +65,11 @@ class PollRepository extends AbstractRepository {
* Search polls in databse. * Search polls in databse.
* *
* @param array $search Array of search : ['id'=>..., 'title'=>..., 'name'=>...] * @param array $search Array of search : ['id'=>..., 'title'=>..., 'name'=>...]
* @param int $start The number of first entry to select
* @param int $limit The number of entries to find
* @return array The found polls * @return array The found polls
*/ */
public function findAll($search) { public function findAll($search, $start, $limit) {
// Polls // Polls
$prepared = $this->prepare(' $prepared = $this->prepare('
SELECT p.*, SELECT p.*,
@ -77,6 +79,7 @@ SELECT p.*,
AND (:title = "" OR p.title LIKE :title) AND (:title = "" OR p.title LIKE :title)
AND (:name = "" OR p.admin_name LIKE :name) AND (:name = "" OR p.admin_name LIKE :name)
ORDER BY p.title ASC ORDER BY p.title ASC
LIMIT :start, :limit
'); ');
$poll = $search['poll'] . '%'; $poll = $search['poll'] . '%';
@ -85,6 +88,8 @@ SELECT p.*,
$prepared->bindParam(':id', $poll, PDO::PARAM_STR); $prepared->bindParam(':id', $poll, PDO::PARAM_STR);
$prepared->bindParam(':title', $title, PDO::PARAM_STR); $prepared->bindParam(':title', $title, PDO::PARAM_STR);
$prepared->bindParam(':name', $name, PDO::PARAM_STR); $prepared->bindParam(':name', $name, PDO::PARAM_STR);
$prepared->bindParam(':start', $start, PDO::PARAM_INT);
$prepared->bindParam(':limit', $limit, PDO::PARAM_INT);
$prepared->execute(); $prepared->execute();
return $prepared->fetchAll(); return $prepared->fetchAll();
@ -106,13 +111,33 @@ SELECT p.*,
/** /**
* Get the total number of polls in databse. * Get the total number of polls in databse.
* *
* @param array $search Array of search : ['id'=>..., 'title'=>..., 'name'=>...]
* @return int The number of polls * @return int The number of polls
*/ */
public function count() { public function count($search = null) {
// Total count // Total count
$stmt = $this->query('SELECT count(1) nb FROM `' . Utils::table('poll') . '`'); $prepared = $this->prepare('
$count = $stmt->fetch(); SELECT count(1) nb
$stmt->closeCursor(); FROM `' . Utils::table('poll') . '` p
WHERE (:id = "" OR p.id LIKE :id)
AND (:title = "" OR p.title LIKE :title)
AND (:name = "" OR p.admin_name LIKE :name)
ORDER BY p.title ASC');
$poll = $search == null ? '' : $search['poll'] . '%';
$title = $search == null ? '' : '%' . $search['title'] . '%';
$name = $search == null ? '' : '%' . $search['name'] . '%';
$prepared->bindParam(':id', $poll, PDO::PARAM_STR);
$prepared->bindParam(':title', $title, PDO::PARAM_STR);
$prepared->bindParam(':name', $name, PDO::PARAM_STR);
$prepared->execute();
$count = $prepared->fetch();
/*echo '---';
print_r($count);
echo '---';
exit;*/
return $count->nb; return $count->nb;
} }

View File

@ -26,11 +26,12 @@ class SuperAdminService {
*/ */
public function findAllPolls($search, $page, $limit) { public function findAllPolls($search, $page, $limit) {
$start = $page * $limit; $start = $page * $limit;
$polls = $this->pollRepository->findAll($search); $polls = $this->pollRepository->findAll($search, $start, $limit);
$count = $this->pollRepository->count($search);
$total = $this->pollRepository->count(); $total = $this->pollRepository->count();
return ['polls' => array_slice($polls, $start, $limit), 'count' => count($polls), 'total' => $total]; return ['polls' => $polls, 'count' => $count, 'total' => $total];
} }
} }

View File

@ -53,7 +53,7 @@
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading"> <div class="panel-heading">
{$count} / {$total} {__('Admin', 'polls in the database at this time')} {if $count == $total}{$count}{else}{$count} / {$total}{/if} {__('Admin', 'polls in the database at this time')}
</div> </div>
<table class="table table-bordered table-polls"> <table class="table table-bordered table-polls">