質問編集履歴
2
phpを削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -62,238 +62,6 @@
|
|
62
62
|
|
63
63
|
```
|
64
64
|
|
65
|
-
```PHP
|
66
|
-
|
67
|
-
pagination.php
|
68
|
-
|
69
|
-
ページネーション本体、これをlinks.php内で呼び出す。
|
70
|
-
|
71
|
-
<?php
|
72
|
-
|
73
|
-
/**
|
74
|
-
|
75
|
-
* ページネーション出力関数
|
76
|
-
|
77
|
-
* $current : 現在のページ
|
78
|
-
|
79
|
-
* $pages : 全ページ数
|
80
|
-
|
81
|
-
* $range : 左右に何ページ表示するか
|
82
|
-
|
83
|
-
* $show_only : 1ページしかない時に表示するかどうか
|
84
|
-
|
85
|
-
*/
|
86
|
-
|
87
|
-
function pagination($current, $pages, $range = 2, $show_only = false) {
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
$current = (!empty($current) && is_numeric($current)) ? $current : 1;
|
92
|
-
|
93
|
-
$pages = (int) $pages;
|
94
|
-
|
95
|
-
$range = (int) $range;
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
$text_first = "First";
|
100
|
-
|
101
|
-
$text_before = "Prev";
|
102
|
-
|
103
|
-
$text_next = "Next";
|
104
|
-
|
105
|
-
$text_last = "Last";
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
//1ページのみで表示設定が true の時
|
110
|
-
|
111
|
-
if ($show_only && $pages === 1) {
|
112
|
-
|
113
|
-
echo '<div class="pagination"><a href="?p=1" class="pager">1</a></div>';
|
114
|
-
|
115
|
-
return;
|
116
|
-
|
117
|
-
}
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
// 0ページ、または1ページで表示設定もない場合
|
122
|
-
|
123
|
-
if ($pages <= 1) return;
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
//2ページ以上の時
|
128
|
-
|
129
|
-
if (1 !== $pages) {
|
130
|
-
|
131
|
-
echo '<div class="pagination">';
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
//「最初へ」の表示
|
136
|
-
|
137
|
-
if ($current > $range + 1) {
|
138
|
-
|
139
|
-
echo '<a href="?p=1" class="first">', $text_first, '</a>';
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
//「前へ」の表示
|
146
|
-
|
147
|
-
if ($current > 1) {
|
148
|
-
|
149
|
-
echo '<a href="?p=', $current - 1,'" class="prev">', $text_before, '</a>';
|
150
|
-
|
151
|
-
}
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
echo '<div class="page-num">';
|
156
|
-
|
157
|
-
for ($i = 1; $i <= $pages; $i++) {
|
158
|
-
|
159
|
-
//$current +- $range 以内であればページ番号を出力
|
160
|
-
|
161
|
-
if ($i <= $current + $range && $i >= $current - $range) {
|
162
|
-
|
163
|
-
echo '<a href="?p=', $i,'" class="pager">', $i, '</a>';
|
164
|
-
|
165
|
-
}
|
166
|
-
|
167
|
-
}
|
168
|
-
|
169
|
-
echo '</div>';
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
//「次へ」 の表示
|
174
|
-
|
175
|
-
if ($current < $pages) {
|
176
|
-
|
177
|
-
echo '<a href="?p=', $current + 1,'" class="next">', $text_next, '</a>';
|
178
|
-
|
179
|
-
}
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
// 「最後へ」 の表示
|
184
|
-
|
185
|
-
if ($current + $range < $pages) {
|
186
|
-
|
187
|
-
echo '<a href="?p=', $pages,'" class="last">', $text_last, '</a>';
|
188
|
-
|
189
|
-
}
|
190
|
-
|
191
|
-
echo '</div>';
|
192
|
-
|
193
|
-
}
|
194
|
-
|
195
|
-
}
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
links.php
|
200
|
-
|
201
|
-
pagination.phpを呼び出し、ページリンクを作成する
|
202
|
-
|
203
|
-
<?php
|
204
|
-
|
205
|
-
require_once('pagination.php');
|
206
|
-
|
207
|
-
//リンクを格納した配列
|
208
|
-
|
209
|
-
$links = array(
|
210
|
-
|
211
|
-
$_SERVER['DOCUMENT_ROOT'] . '/hoge.php',
|
212
|
-
|
213
|
-
$_SERVER['DOCUMENT_ROOT'] . '/hoge2.php',
|
214
|
-
|
215
|
-
・・・略・・・
|
216
|
-
|
217
|
-
$_SERVER['DOCUMENT_ROOT'] . '/hoge100.php',
|
218
|
-
|
219
|
-
);
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
define('MAX_LINK_NUM','3'); //1ページあたりの最大のリンク数
|
224
|
-
|
225
|
-
define('PAGE_RANGE', '2'); //左右に何ページ表示するか
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
$links_num = count($links);
|
230
|
-
|
231
|
-
$max = ceil($links_num / MAX_LINK_NUM);
|
232
|
-
|
233
|
-
$current = 1;
|
234
|
-
|
235
|
-
$page_num = filter_input(INPUT_GET,"p",FILTER_VALIDATE_INT,["options"=>["min_range"=>1,'max_range'=>$max]]);
|
236
|
-
|
237
|
-
if($page_num) {
|
238
|
-
|
239
|
-
$current = $page_num;
|
240
|
-
|
241
|
-
}
|
242
|
-
|
243
|
-
//表示するリンクを抽出
|
244
|
-
|
245
|
-
$start_no = ($current - 1) * MAX_LINK_NUM;
|
246
|
-
|
247
|
-
$disp_data = array_slice($links, $start_no, MAX_LINK_NUM, true);
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
//リンクを表示
|
252
|
-
|
253
|
-
$i = 0;
|
254
|
-
|
255
|
-
$total_count = 0;
|
256
|
-
|
257
|
-
$count_disp_data = count($disp_data);
|
258
|
-
|
259
|
-
foreach($disp_data as $val){
|
260
|
-
|
261
|
-
if($i == 0 ) {
|
262
|
-
|
263
|
-
echo '<div class="post-list-box">';
|
264
|
-
|
265
|
-
}
|
266
|
-
|
267
|
-
include($val);
|
268
|
-
|
269
|
-
$i++;
|
270
|
-
|
271
|
-
$total_count++;
|
272
|
-
|
273
|
-
if($i == 2 || $total_count == $count_disp_data) {
|
274
|
-
|
275
|
-
echo '</div>';
|
276
|
-
|
277
|
-
$i = 0;
|
278
|
-
|
279
|
-
}
|
280
|
-
|
281
|
-
}
|
282
|
-
|
283
|
-
?>
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
<div class="container-pagination">
|
288
|
-
|
289
|
-
全件数 <?php echo $links_num . "件!"; ?>
|
290
|
-
|
291
|
-
<?php pagination($current, $max, PAGE_RANGE, true); ?>
|
292
|
-
|
293
|
-
</div>
|
294
|
-
|
295
|
-
```
|
296
|
-
|
297
65
|
|
298
66
|
|
299
67
|
phpでページネーションを作成し、うまく動作しています。ページ数は動的に変化し、`1 2 3 4 5`のように表示され、クリックしたページへ遷移します。現在、どのページが表示されているのかは、パラメータの`p`によって把握できます。そして、javascriptにて、`p`の値と同じ`aタグ`に`current`クラスを付与し、他とは違うスタイルを適用させるようにしています。これによって、現在表示しているページのリンクは、他のリンクとは異なる見た目となり、どのページが表示されているのかが分かりやすくなっています。
|
1
phpを追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -62,6 +62,238 @@
|
|
62
62
|
|
63
63
|
```
|
64
64
|
|
65
|
+
```PHP
|
66
|
+
|
67
|
+
pagination.php
|
68
|
+
|
69
|
+
ページネーション本体、これをlinks.php内で呼び出す。
|
70
|
+
|
71
|
+
<?php
|
72
|
+
|
73
|
+
/**
|
74
|
+
|
75
|
+
* ページネーション出力関数
|
76
|
+
|
77
|
+
* $current : 現在のページ
|
78
|
+
|
79
|
+
* $pages : 全ページ数
|
80
|
+
|
81
|
+
* $range : 左右に何ページ表示するか
|
82
|
+
|
83
|
+
* $show_only : 1ページしかない時に表示するかどうか
|
84
|
+
|
85
|
+
*/
|
86
|
+
|
87
|
+
function pagination($current, $pages, $range = 2, $show_only = false) {
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
$current = (!empty($current) && is_numeric($current)) ? $current : 1;
|
92
|
+
|
93
|
+
$pages = (int) $pages;
|
94
|
+
|
95
|
+
$range = (int) $range;
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
$text_first = "First";
|
100
|
+
|
101
|
+
$text_before = "Prev";
|
102
|
+
|
103
|
+
$text_next = "Next";
|
104
|
+
|
105
|
+
$text_last = "Last";
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
//1ページのみで表示設定が true の時
|
110
|
+
|
111
|
+
if ($show_only && $pages === 1) {
|
112
|
+
|
113
|
+
echo '<div class="pagination"><a href="?p=1" class="pager">1</a></div>';
|
114
|
+
|
115
|
+
return;
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
// 0ページ、または1ページで表示設定もない場合
|
122
|
+
|
123
|
+
if ($pages <= 1) return;
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
//2ページ以上の時
|
128
|
+
|
129
|
+
if (1 !== $pages) {
|
130
|
+
|
131
|
+
echo '<div class="pagination">';
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
//「最初へ」の表示
|
136
|
+
|
137
|
+
if ($current > $range + 1) {
|
138
|
+
|
139
|
+
echo '<a href="?p=1" class="first">', $text_first, '</a>';
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
//「前へ」の表示
|
146
|
+
|
147
|
+
if ($current > 1) {
|
148
|
+
|
149
|
+
echo '<a href="?p=', $current - 1,'" class="prev">', $text_before, '</a>';
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
echo '<div class="page-num">';
|
156
|
+
|
157
|
+
for ($i = 1; $i <= $pages; $i++) {
|
158
|
+
|
159
|
+
//$current +- $range 以内であればページ番号を出力
|
160
|
+
|
161
|
+
if ($i <= $current + $range && $i >= $current - $range) {
|
162
|
+
|
163
|
+
echo '<a href="?p=', $i,'" class="pager">', $i, '</a>';
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
echo '</div>';
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
//「次へ」 の表示
|
174
|
+
|
175
|
+
if ($current < $pages) {
|
176
|
+
|
177
|
+
echo '<a href="?p=', $current + 1,'" class="next">', $text_next, '</a>';
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
// 「最後へ」 の表示
|
184
|
+
|
185
|
+
if ($current + $range < $pages) {
|
186
|
+
|
187
|
+
echo '<a href="?p=', $pages,'" class="last">', $text_last, '</a>';
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
echo '</div>';
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
links.php
|
200
|
+
|
201
|
+
pagination.phpを呼び出し、ページリンクを作成する
|
202
|
+
|
203
|
+
<?php
|
204
|
+
|
205
|
+
require_once('pagination.php');
|
206
|
+
|
207
|
+
//リンクを格納した配列
|
208
|
+
|
209
|
+
$links = array(
|
210
|
+
|
211
|
+
$_SERVER['DOCUMENT_ROOT'] . '/hoge.php',
|
212
|
+
|
213
|
+
$_SERVER['DOCUMENT_ROOT'] . '/hoge2.php',
|
214
|
+
|
215
|
+
・・・略・・・
|
216
|
+
|
217
|
+
$_SERVER['DOCUMENT_ROOT'] . '/hoge100.php',
|
218
|
+
|
219
|
+
);
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
define('MAX_LINK_NUM','3'); //1ページあたりの最大のリンク数
|
224
|
+
|
225
|
+
define('PAGE_RANGE', '2'); //左右に何ページ表示するか
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
$links_num = count($links);
|
230
|
+
|
231
|
+
$max = ceil($links_num / MAX_LINK_NUM);
|
232
|
+
|
233
|
+
$current = 1;
|
234
|
+
|
235
|
+
$page_num = filter_input(INPUT_GET,"p",FILTER_VALIDATE_INT,["options"=>["min_range"=>1,'max_range'=>$max]]);
|
236
|
+
|
237
|
+
if($page_num) {
|
238
|
+
|
239
|
+
$current = $page_num;
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
//表示するリンクを抽出
|
244
|
+
|
245
|
+
$start_no = ($current - 1) * MAX_LINK_NUM;
|
246
|
+
|
247
|
+
$disp_data = array_slice($links, $start_no, MAX_LINK_NUM, true);
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
//リンクを表示
|
252
|
+
|
253
|
+
$i = 0;
|
254
|
+
|
255
|
+
$total_count = 0;
|
256
|
+
|
257
|
+
$count_disp_data = count($disp_data);
|
258
|
+
|
259
|
+
foreach($disp_data as $val){
|
260
|
+
|
261
|
+
if($i == 0 ) {
|
262
|
+
|
263
|
+
echo '<div class="post-list-box">';
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
include($val);
|
268
|
+
|
269
|
+
$i++;
|
270
|
+
|
271
|
+
$total_count++;
|
272
|
+
|
273
|
+
if($i == 2 || $total_count == $count_disp_data) {
|
274
|
+
|
275
|
+
echo '</div>';
|
276
|
+
|
277
|
+
$i = 0;
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
?>
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
<div class="container-pagination">
|
288
|
+
|
289
|
+
全件数 <?php echo $links_num . "件!"; ?>
|
290
|
+
|
291
|
+
<?php pagination($current, $max, PAGE_RANGE, true); ?>
|
292
|
+
|
293
|
+
</div>
|
294
|
+
|
295
|
+
```
|
296
|
+
|
65
297
|
|
66
298
|
|
67
299
|
phpでページネーションを作成し、うまく動作しています。ページ数は動的に変化し、`1 2 3 4 5`のように表示され、クリックしたページへ遷移します。現在、どのページが表示されているのかは、パラメータの`p`によって把握できます。そして、javascriptにて、`p`の値と同じ`aタグ`に`current`クラスを付与し、他とは違うスタイルを適用させるようにしています。これによって、現在表示しているページのリンクは、他のリンクとは異なる見た目となり、どのページが表示されているのかが分かりやすくなっています。
|