From b26b34df6043ae44259b3460a0cb3c3df367f4f7 Mon Sep 17 00:00:00 2001 From: rootcoma Date: Mon, 25 Nov 2013 18:56:16 -0800 Subject: [PATCH 1/2] Some pagination for web --- php/reports.php | 71 ++++++++++++++++++++++++++++++++++++++++++++++--- php/user.php | 29 ++++++++++++++++++-- 2 files changed, 95 insertions(+), 5 deletions(-) diff --git a/php/reports.php b/php/reports.php index 3689747..ac2727d 100644 --- a/php/reports.php +++ b/php/reports.php @@ -2,6 +2,39 @@ @@ -111,12 +144,20 @@ query('SELECT tr.*, tq.question as original FROM triviareport tr INNER JOIN triviaquestion tq on tq.id=question_num ORDER BY id DESC LIMIT 10'); + $q = $db->prepare('SELECT tr.*, tq.question as original + FROM triviareport tr + INNER JOIN triviaquestion tq + on tq.id=question_num + ORDER BY id DESC LIMIT :offset, :maxResults'); + $qCount = $db->query('SELECT count(id) FROM triviareport'); + $q->execute(array(':offset'=>($reportPage-1) * $maxResults, ':maxResults'=>$maxResults)); if ($q === false) { die("Error: database error: table does not exist\n"); } else { $result = $q->fetchAll(); + $resultCount = $qCount->fetchColumn(); foreach($result as $res) { echo ''; echo '' . $res['id'] . ''; @@ -133,6 +174,10 @@ ?> + paginate(); + ?> @@ -161,12 +206,20 @@ query('SELECT te.*, tq.question as original FROM triviaedit te INNER JOIN triviaquestion tq on tq.id=question_id ORDER BY id DESC LIMIT 10'); + $q = $db->prepare('SELECT te.*, tq.question as original + FROM triviaedit te + INNER JOIN triviaquestion tq + on tq.id=question_id + ORDER BY id DESC LIMIT :offset, :maxResults'); + $q->execute(array(':offset'=>($editPage-1) * $maxResults, ':maxResults'=>$maxResults)); + $qCount = $db->query('SELECT count(id) FROM triviaedit'); if ($q === false) { die("Error: database error: table does not exist\n"); } else { $result = $q->fetchAll(); + $resultCount = $qCount->fetchColumn(); foreach($result as $res) { $isItalic = false; $splitNew = explode('*', $res['question']); @@ -224,6 +277,10 @@ ?> + paginate(); + ?> @@ -251,12 +308,16 @@ query('SELECT tq.* FROM triviatemporaryquestion tq ORDER BY tq.id DESC LIMIT 10'); + $q = $db->prepare('SELECT tq.* FROM triviatemporaryquestion tq ORDER BY tq.id DESC LIMIT :offset, :maxResults'); + $q->execute(array(':offset'=>($newPage-1) * $maxResults, ':maxResults'=>$maxResults)); + $qCount = $db->query('SELECT count(id) FROM triviatemporaryquestion'); if ($q === false) { die("Error: database error: table does not exist\n"); } else { $result = $q->fetchAll(); + $resultCount = $qCount->fetchColumn(); foreach($result as $res) { echo ''; echo '' . $res['id'] . ''; @@ -271,6 +332,10 @@ ?> + paginate(); + ?> diff --git a/php/user.php b/php/user.php index 32993ac..25fda6b 100644 --- a/php/user.php +++ b/php/user.php @@ -2,6 +2,7 @@ @@ -105,6 +118,7 @@ prepare('select tl.username, @@ -113,13 +127,20 @@ from triviauserlog tl where tl.username_canonical like :username group by tl.username_canonical - limit 20 + limit :offset, :maxResults '); - $q->execute(array(':username'=>'%'.$usernameCanonical.'%')); + $qCount = $db->prepare('select + count(distinct(tl.username_canonical)) + from triviauserlog tl + where tl.username_canonical like :username + '); + $q->execute(array(':offset'=>($page-1) * $maxResults, ':maxResults'=>$maxResults, ':username'=>'%'.$usernameCanonical.'%')); + $qCount->execute(array(':username'=>'%'.$usernameCanonical.'%')); if ($q === false) { die("Error: database error: table does not exist\n"); } else { $result = $q->fetchAll(); + $resultCount = $qCount->fetchColumn(); foreach($result as $res) { if(is_null($res['username'])) { echo "
User not found.
"; @@ -168,6 +189,10 @@ ?> + paginate(); + ?> From 78126b9597efc7ee7d8baa7d18f5ed966345ec1a Mon Sep 17 00:00:00 2001 From: rootcoma Date: Mon, 25 Nov 2013 18:56:36 -0800 Subject: [PATCH 2/2] Adding paginator class --- php/pagination.php | 142 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 php/pagination.php diff --git a/php/pagination.php b/php/pagination.php new file mode 100644 index 0000000..50c10a7 --- /dev/null +++ b/php/pagination.php @@ -0,0 +1,142 @@ +currentPage = $currentPage; + $this->totalResults = $totalResults; + if(!is_null($key)) { + $this->key = $key; + } + if(!is_null($resultsPerPage)) { + $this->resultsPerPage = $resultsPerPage; + } + if(!is_null($target)) { + $this->target = $target; + } else { + $this->target = $this->getCurrentPage(); + } + } + + protected function getCurrentPage() { + return $_SERVER['REQUEST_URI']; + } + + protected function replacePageVariable($uri, $pageNumber) { + $pathInfo = parse_url($uri); + if(array_key_exists('query', $pathInfo)) { + $queryString = $pathInfo['query']; + } else { + $queryString = ''; + } + parse_str($queryString, $queryArray); + $queryArray[$this->key] = $pageNumber; + $queryString = http_build_query($queryArray); + $new = $pathInfo['path'] . '?' . $queryString; + return $new; + } + + protected function getMaxPages() { + if($this->totalResults==0) { + return 1; + } + $max = intval($this->totalResults / $this->resultsPerPage); + if($this->totalResults % $this->resultsPerPage > 0) { + $max++; + } + return intval($max); + } + + public function paginate() { + echo ''; + } + } \ No newline at end of file