Add pager

This commit is contained in:
Ruan 2015-07-18 17:51:43 +02:00
parent 2d7176a06a
commit 1c25310fa3
5 changed files with 43 additions and 8 deletions

View File

@ -37,6 +37,7 @@ $page = new Page();
</tr> </tr>
<?php } ?> <?php } ?>
</table> </table>
<?php $page->print_pager("bans.php"); ?>
</div> </div>
</div> </div>
<?php $page->print_footer(); ?> <?php $page->print_footer(); ?>

View File

@ -10,21 +10,37 @@ class Page {
$this->settings = $settings; $this->settings = $settings;
$this->uuid_name_cache = array(); $this->uuid_name_cache = array();
$this->time = microtime(true); $this->time = microtime(true);
} $this->page = 0;
function get_query($table) { if (isset($_GET['page'])) {
$active_query = $this->settings->active_query; $page = $_GET['page']; // user input
$limit = $this->settings->limit_per_page; if (filter_var($page, FILTER_VALIDATE_INT)) {
return "SELECT * FROM $table $active_query GROUP BY $table.id ORDER BY time DESC LIMIT $limit"; $this->page = (int)$page;
}
}
} }
function run_query($table) { function run_query($table) {
try { try {
$result = $this->conn->query($this->get_query($table)); $active_query = $this->settings->active_query;
$limit = $this->settings->limit_per_page;
$offset = 0;
if ($this->settings->show_pager) {
$offset = ($limit * $this->page);
}
$query = "SELECT * FROM $table $active_query GROUP BY $table.id ORDER BY time DESC LIMIT :limit OFFSET :offset";
$st = $this->conn->prepare($query);
$st->bindParam(':offset', $offset, PDO::PARAM_INT);
$st->bindParam(':limit', $limit, PDO::PARAM_INT);
$st->execute();
return $st;
} catch (PDOException $ex) { } catch (PDOException $ex) {
die($ex->getMessage()); die($ex->getMessage());
} }
return $result;
} }
function get_avatar($name) { function get_avatar($name) {
@ -107,6 +123,19 @@ class Page {
'); ');
} }
function print_pager($page) {
if (!$this->settings->show_pager) return;
$prev = $this->page - 1;
$next = $this->page + 1;
$pager_prev = "<div style=\"float:left;\">&lt;&lt;</div>";
if ($this->page > 0) {
$pager_prev = "<a href=\"$page?page=$prev\">$pager_prev</a>";
}
$pager_next = "<a href=\"$page?page=$next\"><div style=\"float: right;\">&gt;&gt;</div></a>";
echo "$pager_prev $pager_next";
}
function print_footer() { function print_footer() {
include './includes/footer.php'; include './includes/footer.php';
$time = microtime(true) - $this->time; $time = microtime(true) - $this->time;

View File

@ -18,6 +18,9 @@ final class Settings {
// Show inactive bans? Removed bans will show (Unbanned), mutes will show (Unmuted), warnings will show (Expired). // Show inactive bans? Removed bans will show (Unbanned), mutes will show (Unmuted), warnings will show (Expired).
$this->show_inactive_bans = true; $this->show_inactive_bans = true;
// Show pager? This allows users to page through the list of bans.
$this->show_pager = true;
// Amount of bans/mutes/warnings to show on each page // Amount of bans/mutes/warnings to show on each page
$this->limit_per_page = 20; $this->limit_per_page = 20;

View File

@ -36,6 +36,7 @@ $page = new Page();
</tr> </tr>
<?php } ?> <?php } ?>
</table> </table>
<?php $page->print_pager("mutes.php"); ?>
</div> </div>
</div> </div>
<?php $page->print_footer(); ?> <?php $page->print_footer(); ?>

View File

@ -38,6 +38,7 @@ $page = new Page();
</tr> </tr>
<?php } ?> <?php } ?>
</table> </table>
<?php $page->print_pager("warnings.php"); ?>
</div> </div>
</div> </div>
<?php $page->print_footer(); ?> <?php $page->print_footer(); ?>