|
111 | 111 |
|
112 | 112 | // Build pagination div.
|
113 | 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 . ' '; |
122 |
| - } else { |
123 |
| - $pagination .= '<a href="' . $url . '&page=' . $page_num . '">' . $page_num . "</a> "; |
| 114 | + $prev = $page_num_request - 1; |
| 115 | + $next = $page_num_request + 1; |
| 116 | + $lastpage = ceil($size / $count_elements_page); |
| 117 | + $lpm1 = $lastpage - 1; |
| 118 | + $adjacents = 3; |
| 119 | + $pagination = '<div style="width: inherit; word-wrap: break-word;">'; |
| 120 | + $url = preg_replace('/&page=(\d+)/i', '', $_SERVER['REQUEST_URI']); |
| 121 | + |
| 122 | + if ($page_num_request > 1) $pagination .= "<a href=\"$url&page=$prev\">←</a> "; else |
| 123 | + $pagination .= "← "; |
| 124 | + |
| 125 | + if ($lastpage < 7 + ($adjacents * 2)) { //not enough pages to bother breaking it up |
| 126 | + for ($counter = 1; $counter <= $lastpage; $counter++) { |
| 127 | + if ($counter == $page_num_request) $pagination .= $page_num_request . ' '; else |
| 128 | + $pagination .= "<a href=\"$url&page=$counter\">$counter</a> "; |
| 129 | + } |
| 130 | + } elseif ($lastpage > 5 + ($adjacents * 2)) { //enough pages to hide some |
| 131 | + |
| 132 | + if ($page_num_request < 1 + ($adjacents * 2)) { //close to beginning; only hide later pages |
| 133 | + for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { |
| 134 | + if ($counter == $page_num_request) $pagination .= $page_num_request . ' '; else |
| 135 | + $pagination .= "<a href=\"$url&page=$counter\">$counter</a> "; |
| 136 | + } |
| 137 | + $pagination .= "... "; |
| 138 | + $pagination .= "<a href=\"$url&page=$lpm1\">$lpm1</a> "; |
| 139 | + $pagination .= "<a href=\"$url&page=$lastpage\">$lastpage</a> "; |
| 140 | + } elseif ($lastpage - ($adjacents * 2) > $page_num_request && $page_num_request > ($adjacents * 2)) { //in middle; hide some front and some back |
| 141 | + $pagination .= "<a href=\"$url&page=1\">1</a> "; |
| 142 | + $pagination .= "<a href=\"$url&page=2\">2</a> "; |
| 143 | + $pagination .= "... "; |
| 144 | + for ($counter = $page_num_request - $adjacents; $counter <= $page_num_request + $adjacents; $counter++) { |
| 145 | + if ($counter == $page_num_request) $pagination .= $page_num_request . ' '; else |
| 146 | + $pagination .= "<a href=\"$url&page=$counter\">$counter</a> "; |
| 147 | + } |
| 148 | + $pagination .= "... "; |
| 149 | + $pagination .= "<a href=\"$url&page=$lpm1\">$lpm1</a> "; |
| 150 | + $pagination .= "<a href=\"$url&page=$lastpage\">$lastpage</a> "; |
| 151 | + } else { //close to end; only hide early pages |
| 152 | + $pagination .= "<a href=\"$url&page=1\">1</a> "; |
| 153 | + $pagination .= "<a href=\"$url&page=2\">2</a> "; |
| 154 | + $pagination .= "... "; |
| 155 | + for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { |
| 156 | + if ($counter == $page_num_request) $pagination .= $page_num_request . ' '; else |
| 157 | + $pagination .= "<a href=\"$url&page=$counter\">$counter</a> "; |
| 158 | + } |
| 159 | + } |
124 | 160 | }
|
125 |
| - } |
126 |
| - |
127 |
| - $pagination .= '</div>'; |
| 161 | + if ($page_num_request < $counter - 1) $pagination .= "<a href=\"$url&page=$next\">→</a> "; else |
| 162 | + $pagination .= "→ "; |
| 163 | + $pagination .= "</div>"; |
128 | 164 | }
|
129 | 165 |
|
130 |
| - |
131 | 166 | if (isset($pagination)) {
|
132 |
| - echo $pagination; |
| 167 | + echo $pagination; |
133 | 168 | }
|
134 | 169 |
|
135 | 170 |
|
|
0 commit comments