回答編集履歴
1
追記
test
CHANGED
@@ -3,3 +3,115 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
ページネーション以前でとまってる。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
```html
|
10
|
+
|
11
|
+
<?php
|
12
|
+
|
13
|
+
if (isset($_GET['category'])) {
|
14
|
+
|
15
|
+
$category_id = $_GET['category'];
|
16
|
+
|
17
|
+
}
|
18
|
+
|
19
|
+
// パラメータをSQLにそのままセットするな!SQLインジェクション脆弱性あり!!
|
20
|
+
|
21
|
+
$query = "SELECT * FROM posts WHERE category_id = $category_id ORDER BY post_id DESC LIMIT 0, 5";
|
22
|
+
|
23
|
+
$posts_query = mysqli_query($con, $query);
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
while ($row = mysqli_fetch_assoc($posts_query)) {
|
28
|
+
|
29
|
+
$post_id = $row['post_id'];
|
30
|
+
|
31
|
+
$post_title = $row['post_title'];
|
32
|
+
|
33
|
+
$post_title = $row['post_author']; // 変数名チェックせよ!
|
34
|
+
|
35
|
+
?>
|
36
|
+
|
37
|
+
<div class="list">
|
38
|
+
|
39
|
+
<h3><?php echo $post_title; ?></h3>
|
40
|
+
|
41
|
+
<p><?php echo $post_author; ?><br></p>
|
42
|
+
|
43
|
+
</div>
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
<?php
|
48
|
+
|
49
|
+
//ぺージネーション作成
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
// filter_input 使う filter_input(INPUT_GET, 'page')
|
54
|
+
|
55
|
+
if (isset($_GET['page'])) {
|
56
|
+
|
57
|
+
$page = $_GET['page'];
|
58
|
+
|
59
|
+
} else {
|
60
|
+
|
61
|
+
$page = "";
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
if ($page == "" || $page == 1) {
|
68
|
+
|
69
|
+
$page_1 = 0;
|
70
|
+
|
71
|
+
} else {
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
$page_1 = ($page * 5) - 5;
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
// パラメータをSQLにそのままセットするな!SQLインジェクション脆弱性あり!!
|
82
|
+
|
83
|
+
// ループの中でSQLを投げるな。JOIN使え。
|
84
|
+
|
85
|
+
$query_count = "SELECT * FROM posts WHERE category_id = $category_id ORDER BY post_id DESC";
|
86
|
+
|
87
|
+
$find_count = mysqli_query($con, $query_count);
|
88
|
+
|
89
|
+
$count = mysqli_num_rows($find_count);
|
90
|
+
|
91
|
+
$count = ceil($count / 5);
|
92
|
+
|
93
|
+
?>
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
<?php } ?>
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
<ul class="pager">
|
102
|
+
|
103
|
+
<?php
|
104
|
+
|
105
|
+
for ($i = 1; $i <= $count; $i++) {
|
106
|
+
|
107
|
+
echo "<li><a href='category.php?category=$category_id&page={$i}'>{$i}</a></li>";
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
?>
|
112
|
+
|
113
|
+
</ul>
|
114
|
+
|
115
|
+
```
|
116
|
+
|
117
|
+
|