Category : PHP
jQuery AJAXとPHPを組み合わせたページネーションと検索
index.php
<html>
<head>
<title>AJAX Pagination</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" />
<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2 class="text-center">jQuery AJAXとPHPを組み合わせたページネーションと検索</h2>
<br />
<div class="form-outline mb-4">
<h6 class="mt-5">検索</h6>
<input type="text" id="search" class="form-control" placeholder="名前を入力してください"
style="width: 250px; display: inline-block; margin-bottom: 20px;">
</div>
<div id="result"></div>
<div id="pagination"></div>
</div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
let currentPage = 1;
/* 初期表示 */
$(document).ready(function() {
loadData(1);
});
/* 検索入力 */
$("#search").on("keyup", function() {
loadData(1);
});
/* ページ切替 */
$(document).on("click", ".pagination a", function(e) {
e.preventDefault();
let page = $(this).data("page");
loadData(page);
});
/* AJAX */
function loadData(page) {
let keyword = $("#search").val();
$.ajax({
url: "search.php",
type: "POST",
data: {
page: page,
keyword: keyword
},
success: function(response) {
let data = JSON.parse(response);
$("#result").html(data.html);
$("#pagination").html(data.pagination);
}
});
}
</script>
</body>
</html>
search.php
<?php
include "con.php";
$page = trim($_POST['page'] ?? 1);
$keyword = mb_trim($_POST['keyword'] ?? '');
$limit = 10;
$offset = ($page - 1) * $limit;
/* 検索条件 */
$where = "";
if ($keyword != "") {
$keyword = mysqli_real_escape_string($connection, $keyword);
$where = "WHERE name LIKE '%$keyword%'";
}
/* データ取得 */
$sql = "SELECT * FROM prefectures $where LIMIT $limit OFFSET $offset";
$result = mysqli_query($connection, $sql);
$html ='<table class="table table-bordered table-striped">
<thead style="background-color:#FFA500;color:#fff;">
<th>No</th>
<th>都道府県</th>
<th>県庁所在地</th>
</thead>';
while ($row = mysqli_fetch_assoc($result)) {
$html .= "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['capital']}</td>
</tr>";
}
$output .="</table>";
/* 総件数 */
$count_sql = "SELECT COUNT(*) as cnt FROM prefectures $where";
$count_result = mysqli_query($connection, $count_sql);
$total = mysqli_fetch_assoc($count_result)['cnt'];
$total_pages = ceil($total / $limit);
$range = 2; // 現在ページの前後に表示する数
$start = max(1, $page - $range);
$end = min($total_pages, $page + $range);
/* pagination */
$pagination = '<ul class="pagination justify-content-center">';
if ($page > 1) {
$prev = $page - 1;
$pagination .= "<li><a href='#' data-page='{$prev}'>前へ</a></li>";
}
if ($start > 1) {
$pagination .= "<li><a href='#' data-page='1'>1</a></li>";
if ($start > 2) {
$pagination .= "<li><span>...</span></li>";
}
}
for ($i = $start; $i <= $end; $i++) {
$active = ($i == $page) ? "active" : "";
$pagination .= "<li class='$active'><a href='#' data-page='$i'>$i</a></li>";
}
if ($end < $total_pages) {
if ($end < $total_pages - 1) {
$pagination .= "<li><span>...</span></li>";
}
$pagination .= "<li><a href='#' data-page='{$total_pages}'>{$total_pages}</a></li>";
}
/* 次へ */
if ($page < $total_pages) {
$next = $page + 1;
$pagination .= "<li><a href='#' data-page='{$next}'>次へ</a></li>";
}
$pagination .= '</ul>';
/* JSON返す */
echo json_encode([
"html" => $html,
"pagination" => $pagination
]);
PHPデベロッパー