Merge pull request #170 from rootcoma/pagination_web
Some pagination for web
This commit is contained in:
commit
bc26acbba2
|
|
@ -0,0 +1,142 @@
|
|||
<?php
|
||||
class Paginator
|
||||
{
|
||||
protected $key = 'page';
|
||||
protected $target = '';
|
||||
protected $next = 'Next »';
|
||||
protected $previous = '« Previous';
|
||||
protected $crumbs = 5;
|
||||
protected $resultsPerPage = 10;
|
||||
protected $currentPage;
|
||||
protected $totalResults;
|
||||
|
||||
public function __construct($currentPage, $totalResults, $resultsPerPage=null, $key=null, $target=null) {
|
||||
$this->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 '<div class="pagination"><ul>';
|
||||
$startCrumb = $this->currentPage - ($this->crumbs-1)/2;
|
||||
if($startCrumb < 1) {
|
||||
$startCrumb = 1;
|
||||
}
|
||||
|
||||
$endCrumb = $startCrumb + $this->crumbs-1;
|
||||
if($endCrumb > $this->getMaxPages()) {
|
||||
$endCrumb = $this->getMaxPages();
|
||||
}
|
||||
|
||||
while($endCrumb - $startCrumb < $this->crumbs - 1) {
|
||||
if($startCrumb <= 1) {
|
||||
break;
|
||||
}
|
||||
$startCrumb--;
|
||||
}
|
||||
|
||||
$startDots = false;
|
||||
if($startCrumb > 1) {
|
||||
$startDots = true;
|
||||
}
|
||||
$endDots = false;
|
||||
if($endCrumb < $this->getMaxPages()) {
|
||||
$endDots = true;
|
||||
}
|
||||
|
||||
if($this->currentPage > 1) {
|
||||
echo '<li>';
|
||||
echo '<a href="';
|
||||
echo $this->replacePageVariable($this->target, $this->currentPage-1);
|
||||
echo '">';
|
||||
} else {
|
||||
echo '<li class="disabled"><span>';
|
||||
}
|
||||
echo $this->previous;
|
||||
if($this->currentPage != 1) {
|
||||
echo '</a></li>';
|
||||
} else {
|
||||
echo '</span></li>';
|
||||
}
|
||||
|
||||
if($startDots){
|
||||
echo '<li class="disabled"><span>...</span></li>';
|
||||
}
|
||||
|
||||
for($i=$startCrumb;$i<=$endCrumb;$i++) {
|
||||
if($i!=$this->currentPage){
|
||||
echo '<li>';
|
||||
echo '<a href="';
|
||||
echo $this->replacePageVariable($this->target, $i);
|
||||
echo '">';
|
||||
} else {
|
||||
echo '<li class="active"><span>';
|
||||
}
|
||||
echo $i;
|
||||
if($i!=$this->currentPage){
|
||||
echo '</a></li>';
|
||||
} else {
|
||||
echo '</span></li>';
|
||||
}
|
||||
}
|
||||
|
||||
if($endDots){
|
||||
echo '<li class="disabled"><span>...</span></li>';
|
||||
}
|
||||
|
||||
if($this->currentPage < $this->getMaxPages()) {
|
||||
echo '<li>';
|
||||
echo '<a href="';
|
||||
echo $this->replacePageVariable($this->target, $this->currentPage+1);
|
||||
echo '">';
|
||||
} else {
|
||||
echo '<li class="disabled"><span>';
|
||||
}
|
||||
echo $this->next;
|
||||
if($this->currentPage != $this->getMaxPages()) {
|
||||
echo '</a></li>';
|
||||
} else {
|
||||
echo '</span></li>';
|
||||
}
|
||||
echo '</ul></div>';
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,39 @@
|
|||
<html lang="en">
|
||||
<?php
|
||||
include('config.php');
|
||||
include('pagination.php');
|
||||
|
||||
if(array_key_exists('rp', $_GET)) {
|
||||
$reportPage = $_GET['rp'];
|
||||
}
|
||||
if(!isset($reportPage)) {
|
||||
$reportPage = 1;
|
||||
}
|
||||
if($reportPage < 1) {
|
||||
$reportPage = 1;
|
||||
}
|
||||
|
||||
if(array_key_exists('ep', $_GET)) {
|
||||
$editPage = $_GET['ep'];
|
||||
}
|
||||
if(!isset($editPage)) {
|
||||
$editPage = 1;
|
||||
}
|
||||
if($editPage < 1) {
|
||||
$editPage = 1;
|
||||
}
|
||||
|
||||
if(array_key_exists('np', $_GET)) {
|
||||
$newPage = $_GET['np'];
|
||||
}
|
||||
if(!isset($newPage)) {
|
||||
$newPage = 1;
|
||||
}
|
||||
if($newPage < 1) {
|
||||
$newPage = 1;
|
||||
}
|
||||
|
||||
$maxResults = 5;
|
||||
?>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
|
|
@ -111,12 +144,20 @@
|
|||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
$resultCount = 0;
|
||||
if ($db) {
|
||||
$q = $db->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 '<tr>';
|
||||
echo '<td>' . $res['id'] . '</td>';
|
||||
|
|
@ -133,6 +174,10 @@
|
|||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php
|
||||
$pagination = new Paginator($reportPage, $resultCount, $maxResults, 'rp');
|
||||
$pagination->paginate();
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -161,12 +206,20 @@
|
|||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
$resultCount = 0;
|
||||
if ($db) {
|
||||
$q = $db->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 @@
|
|||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php
|
||||
$pagination = new Paginator($editPage, $resultCount, $maxResults, 'ep');
|
||||
$pagination->paginate();
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -251,12 +308,16 @@
|
|||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
$resultCount = 0;
|
||||
if ($db) {
|
||||
$q = $db->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 '<tr>';
|
||||
echo '<td>' . $res['id'] . '</td>';
|
||||
|
|
@ -271,6 +332,10 @@
|
|||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php
|
||||
$pagination = new Paginator($newPage, $resultCount, $maxResults, 'np');
|
||||
$pagination->paginate();
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
29
php/user.php
29
php/user.php
|
|
@ -2,6 +2,7 @@
|
|||
<html lang="en">
|
||||
<?php
|
||||
include('config.php');
|
||||
include('pagination.php');
|
||||
if(array_key_exists('username', $_GET)) {
|
||||
// Convert username to lowercase in irc
|
||||
$username = $_GET['username'];
|
||||
|
|
@ -12,6 +13,18 @@
|
|||
$username = '';
|
||||
$usernameCanonical = '';
|
||||
}
|
||||
|
||||
if(array_key_exists('page', $_GET)) {
|
||||
$page = $_GET['page'];
|
||||
}
|
||||
if(!isset($page)) {
|
||||
$page = 1;
|
||||
}
|
||||
if($page < 1) {
|
||||
$page = 1;
|
||||
}
|
||||
|
||||
$maxResults = 10;
|
||||
?>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
|
|
@ -105,6 +118,7 @@
|
|||
<input type="submit"></input>
|
||||
</form>
|
||||
<?php
|
||||
$resultCount = 0;
|
||||
if ($db) {
|
||||
$q = $db->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 "<div class='alert alert-error'>User not found.</div>";
|
||||
|
|
@ -168,6 +189,10 @@
|
|||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php
|
||||
$pagination = new Paginator($page, $resultCount, $maxResults);
|
||||
$pagination->paginate();
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue