From 1c25310fa35fb7e5e3f2d00173ee469b91572d94 Mon Sep 17 00:00:00 2001 From: Ruan Date: Sat, 18 Jul 2015 17:51:43 +0200 Subject: [PATCH] Add pager --- bans.php | 1 + includes/page.php | 45 +++++++++++++++++++++++++++++++++++-------- includes/settings.php | 3 +++ mutes.php | 1 + warnings.php | 1 + 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/bans.php b/bans.php index 8260d22..21b3c0f 100644 --- a/bans.php +++ b/bans.php @@ -37,6 +37,7 @@ $page = new Page(); + print_pager("bans.php"); ?> print_footer(); ?> diff --git a/includes/page.php b/includes/page.php index 8618bf7..f97ca34 100644 --- a/includes/page.php +++ b/includes/page.php @@ -10,25 +10,41 @@ class Page { $this->settings = $settings; $this->uuid_name_cache = array(); $this->time = microtime(true); - } + $this->page = 0; - function get_query($table) { - $active_query = $this->settings->active_query; - $limit = $this->settings->limit_per_page; - return "SELECT * FROM $table $active_query GROUP BY $table.id ORDER BY time DESC LIMIT $limit"; + if (isset($_GET['page'])) { + $page = $_GET['page']; // user input + if (filter_var($page, FILTER_VALIDATE_INT)) { + $this->page = (int)$page; + } + } } function run_query($table) { 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) { die($ex->getMessage()); } - return $result; } function get_avatar($name) { - return "$name"; + return "$name"; } function get_name($uuid) { @@ -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 = "
<<
"; + if ($this->page > 0) { + $pager_prev = "$pager_prev"; + } + $pager_next = "
>>
"; + echo "$pager_prev $pager_next"; + } + function print_footer() { include './includes/footer.php'; $time = microtime(true) - $this->time; diff --git a/includes/settings.php b/includes/settings.php index c53c08e..317cfc2 100644 --- a/includes/settings.php +++ b/includes/settings.php @@ -18,6 +18,9 @@ final class Settings { // Show inactive bans? Removed bans will show (Unbanned), mutes will show (Unmuted), warnings will show (Expired). $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 $this->limit_per_page = 20; diff --git a/mutes.php b/mutes.php index d1f6d82..1439928 100644 --- a/mutes.php +++ b/mutes.php @@ -36,6 +36,7 @@ $page = new Page(); + print_pager("mutes.php"); ?> print_footer(); ?> diff --git a/warnings.php b/warnings.php index 16b73f5..ab6560f 100644 --- a/warnings.php +++ b/warnings.php @@ -38,6 +38,7 @@ $page = new Page(); + print_pager("warnings.php"); ?> print_footer(); ?>