Skip to content

Commit 2ff5ba5

Browse files
committed
ページャー動くところまでは完成
1 parent d6ad9c0 commit 2ff5ba5

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

app/Http/Controllers/PostController.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,25 @@ class PostController extends Controller
99
{
1010
private $post;
1111

12-
public function __construct(PostService $post_service)
12+
public function __construct(PostService $post_service, Request $request)
1313
{
1414
$this->post = $post_service;
15+
$this->post->setPagerInfo($request->input('page_id'));
1516
}
1617

1718
public function index()
1819
{
19-
$id = 1;
20+
$userId = 1;
2021
$posts = [];
2122

2223
$shopDetails = $this->post->getShopDetails();
23-
$Favorites = $this->post->getFavorites($id);
24+
$favorites = $this->post->getFavorites($userId);
25+
$pagerInfo = $this->post->getPagerInfo();
2426

2527
$posts = [
2628
'shopDetails' => $shopDetails,
27-
'Favorites' => $Favorites
29+
'favorites' => $favorites,
30+
'pagerInfo' => $pagerInfo,
2831
];
2932

3033
return View('posts.index', ['posts' => $posts]);

app/Services/PostService.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55

66
class PostService
77
{
8+
private $maxPage; // 最大ページ数
9+
private $nowPage; // 現在のページ番号
10+
11+
/**
12+
* ページャー情報をセットする
13+
*/
14+
public function setPagerInfo($pageId) {
15+
$this->nowPage = $pageId;
16+
}
17+
18+
/**
19+
* ページャーの情報を返す
20+
*/
21+
public function getPagerInfo() {
22+
return ['maxPage' => $this->maxPage, 'nowPage' => $this->nowPage];
23+
}
824

925
/**
1026
* 店舗情報を取得する
@@ -53,7 +69,7 @@ private function getShopInfo(): array
5369
$sql .= 'GROUP BY posts.id, posts.shopname, posts.tags, posts.deleted_flg'.PHP_EOL;
5470
$sql .= 'ORDER BY avg_rating DESC'.PHP_EOL;
5571

56-
return DB::select($sql);
72+
return $this->pager(DB::select($sql));
5773
}
5874

5975
/**
@@ -81,4 +97,28 @@ public function getFavorites(int $user_id): array
8197

8298
return DB::select($sql, ['user_id' => $user_id]);
8399
}
100+
101+
/**
102+
* ページャー
103+
* @param array $shopData
104+
* @return array 表示用データ
105+
*/
106+
private function pager(array $shopData): array
107+
{
108+
$max = 10; // 1ページの最大表示件数
109+
110+
$shopNum = count($shopData); // 現在の合計店舗数
111+
112+
$this->maxPage = ceil($shopNum / $max); // 最大ページ数
113+
114+
if (!isset($this->nowPage)) {
115+
$this->nowPage = 1; // 特に指定されていなければ1ページ目を取得する
116+
}
117+
118+
$startNo = ($this->nowPage - 1) * $max; // 開始ページ番号を取得する
119+
120+
$dispData = array_slice($shopData, $startNo, $max, true);
121+
122+
return $dispData;
123+
}
84124
}

resources/views/posts/index.blade.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,32 @@
6161
border-radius: 12px;
6262
font-size: 12px;
6363
}
64-
/* ハートマーク */
64+
/* いいね(ハート) */
6565
.likes {
6666
font-size: 18px;
6767
color: red;
6868
display: flex;
6969
align-items: center;
7070
gap: 5px;
7171
}
72+
/* ページネーション */
73+
.pagination {
74+
display: flex;
75+
justify-content: center;
76+
margin-top: 20px;
77+
}
78+
.pagination a,
79+
.pagination span {
80+
margin: 0 5px;
81+
padding: 8px 12px;
82+
text-decoration: none;
83+
background: #007bff;
84+
color: #fff;
85+
border-radius: 5px;
86+
}
87+
.pagination .active {
88+
background: #0056b3;
89+
}
7290
</style>
7391
</head>
7492
<body>
@@ -97,6 +115,17 @@
97115
</div>
98116
</div>
99117
@endforeach
118+
119+
<!-- ページネーション -->
120+
<div class="pagination">
121+
@for ($i = 1; $i <= $posts['pagerInfo']['maxPage']; $i++)
122+
@if ($i == $posts['pagerInfo']['nowPage'])
123+
<span class="active">{{ $i }}</span>
124+
@else
125+
<a href="?page_id={{ $i }}">{{ $i }}</a>
126+
@endif
127+
@endfor
128+
</div>
100129
</div>
101130
</body>
102131
</html>

0 commit comments

Comments
 (0)