Skip to content

Commit 907acc8

Browse files
Added list support to pagination
1 parent 7e603a5 commit 907acc8

File tree

2 files changed

+43
-35
lines changed

2 files changed

+43
-35
lines changed

includes/config.sample.inc.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353

5454
// You can ignore settings below this point.
5555

56-
'maxkeylen' => 100,
57-
'count_elements_page' => 10
56+
'maxkeylen' => 100,
57+
'count_elements_page' => 100
5858
);
5959

6060
?>

view.php

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
require_once 'includes/common.inc.php';
44

5-
$count_elements_page = isset($config['count_elements_page']) ? $config['count_elements_page'] : false;
6-
$page_num_request = isset($_GET['page']) ? (int)$_GET['page'] : 1;
7-
$page_num_request = $page_num_request === 0 ? 1 : $page_num_request;
8-
95
$page['css'][] = 'frame';
106
$page['js'][] = 'frame';
117

@@ -27,6 +23,11 @@
2723
$type = $redis->type($_GET['key']);
2824
$exists = $redis->exists($_GET['key']);
2925

26+
$count_elements_page = isset($config['count_elements_page']) ? $config['count_elements_page'] : false;
27+
$page_num_request = isset($_GET['page']) ? (int)$_GET['page'] : 1;
28+
$page_num_request = $page_num_request === 0 ? 1 : $page_num_request;
29+
30+
3031

3132
?>
3233
<h2><?php echo format_html($_GET['key'])?>
@@ -68,9 +69,6 @@
6869
case 'hash':
6970
$values = $redis->hGetAll($_GET['key']);
7071
$size = count($values);
71-
if($count_elements_page !== false) {
72-
$values = array_slice($values, $count_elements_page*($page_num_request-1), $count_elements_page);
73-
}
7472
break;
7573

7674
case 'list':
@@ -80,20 +78,17 @@
8078
case 'set':
8179
$values = $redis->sMembers($_GET['key']);
8280
$size = count($values);
83-
if($count_elements_page !== false) {
84-
$values = array_slice($values, $count_elements_page*($page_num_request-1), $count_elements_page);
85-
}
8681
break;
8782

8883
case 'zset':
8984
$values = $redis->zRange($_GET['key'], 0, -1);
9085
$size = count($values);
91-
if($count_elements_page !== false) {
92-
$values = array_slice($values, $count_elements_page*($page_num_request-1), $count_elements_page);
93-
}
9486
break;
9587
}
96-
88+
89+
if (isset($values) && ($count_elements_page !== false)) {
90+
$values = array_slice($values, $count_elements_page * ($page_num_request - 1), $count_elements_page);
91+
}
9792

9893
?>
9994
<table>
@@ -113,27 +108,31 @@
113108
<p>
114109
<?php
115110

116-
$pagging = '';
117-
// make pagging div
118-
if($count_elements_page !== false && in_array($type, array('hash', 'set', 'zset')) && $size > $count_elements_page) {
119-
$pagging .= '<div style="width: inherit; word-wrap: break-word;">';
120-
$url = preg_replace('/&page=(\d+)/i', '', $_SERVER['REQUEST_URI']);
121-
for ($i = 0; $i < ceil($size/$count_elements_page); ++$i) {
122-
$page_num = $i+1;
123-
if($page_num === $page_num_request) {
124-
$pagging .= $page_num.'&nbsp;';
125-
}
126-
else {
127-
$pagging .= '<a href="'.$url.'&page='.$page_num.'">'.$page_num."</a>&nbsp;";
111+
112+
// Build pagination div.
113+
if (($count_elements_page !== false) && in_array($type, array('hash', 'list', 'set', 'zset')) && ($size > $count_elements_page)) {
114+
$pagination = '<div style="width: inherit; word-wrap: break-word;">';
115+
$url = preg_replace('/&page=(\d+)/i', '', $_SERVER['REQUEST_URI']);
116+
117+
for ($i = 0; $i < ceil($size / $count_elements_page); ++$i) {
118+
$page_num = $i + 1;
119+
120+
if ($page_num === $page_num_request) {
121+
$pagination .= $page_num . '&nbsp;';
122+
} else {
123+
$pagination .= '<a href="' . $url . '&page=' . $page_num . '">' . $page_num . "</a>&nbsp;";
128124
}
129125
}
130-
$pagging .= '</div>';
126+
127+
$pagination .= '</div>';
131128
}
132129

133-
if(!empty($pagging)) {
134-
echo $pagging;
130+
131+
if (isset($pagination)) {
132+
echo $pagination;
135133
}
136134

135+
137136
// String
138137
if ($type == 'string') { ?>
139138

@@ -172,8 +171,17 @@
172171
<table>
173172
<tr><th><div>Index</div></th><th><div>Value</div></th><th><div>&nbsp;</div></th><th><div>&nbsp;</div></th></tr>
174173

175-
<?php for ($i = 0; $i < $size; ++$i) {
176-
$value = $redis->lIndex($_GET['key'], $i);
174+
<?php
175+
if (($count_elements_page === false) && ($size > $count_elements_page)) {
176+
$start = 0;
177+
$end = $size;
178+
} else {
179+
$start = $count_elements_page * ($page_num_request - 1);
180+
$end = min($start + $count_elements_page, $size);
181+
}
182+
183+
for ($i = $start; $i < $end; ++$i) {
184+
$value = $redis->lIndex($_GET['key'], $i);
177185
?>
178186
<tr <?php echo $alt ? 'class="alt"' : ''?>><td><div><?php echo $i?></div></td><td><div><?php echo nl2br(format_html($value))?></div></td><td><div>
179187
<a href="edit.php?s=<?php echo $server['id']?>&amp;type=list&amp;key=<?php echo urlencode($_GET['key'])?>&amp;index=<?php echo $i?>"><img src="images/edit.png" width="16" height="16" title="Edit" alt="[E]"></a>
@@ -233,8 +241,8 @@
233241
</p>
234242
<?php }
235243

236-
if(!empty($pagging)) {
237-
echo $pagging;
244+
if (isset($pagination)) {
245+
echo $pagination;
238246
}
239247

240248
require 'includes/footer.inc.php';

0 commit comments

Comments
 (0)