我试图添加分页,即使代码看似正确,页面也只显示前10个结果(我想要每页10个结果)。如下所示,问题似乎是在$_GET
方法中创建的。
这是ajax:
function displayAllTopics() {
$.ajax({
url: "requests.php",
type: "GET",
data: {
keyPagination: "displayAllTopics"
},
dataType: "json",
success: function(response) {
$("#topicArea").append(response.response_message);
$("#pagination").append(response.pagination);
}
});
}
以下是requests.php
文件:
if(isset($_GET['keyPagination'])) {
if($_GET['keyPagination'] == "displayAllTopics") {
$sql = "SELECT * FROM travel";
$result = mysqli_query($conn, $sql);
$count_topics = mysqli_num_rows($result);
if($count_topics < 1) {
exit('There are no topics yet.');
}
if(isset($_GET['page'])) {
$page = $_GET['page'];
}
else {
$page = 1;
}
//results per page
$results_per_page = 10;
//number of pages
$offset = ($page-1) * $results_per_page;
$number_of_pages = ceil($count_topics / $results_per_page);
$sql_pagination = "SELECT * FROM travel LIMIT $offset, $results_per_page";
$results_pagination = mysqli_query($conn, $sql_pagination);
$response_message = "";
$pagination = "";
while($row = mysqli_fetch_assoc($results_pagination)) {
$user_id = $row['user_id'];
$travel_subject_id = $row['travel_subject_id'];
$travel_subject_date = $row['travel_subject_date'];
$travel_title = $row['travel_subject_title'];
$travel_body = $row['travel_subject_body'];
$travel_comments_count = $row['travel_comments_count'];
$travel_views = $row['travel_subject_views'];
$travel_subject_date_format = date('j M y H:i', strtotime($travel_subject_date));
$sql_user = "SELECT * FROM forum_users WHERE user_id='$user_id'";
$result_user = mysqli_query($conn, $sql_user);
if($row_user = mysqli_fetch_assoc($result_user)) {
$username = $row_user['username'];
$response_message .= "<tr>
<td><a href='travel_subject.php?id=".$travel_subject_id."' onclick='countview(".$travel_subject_id.");'><img src='topic.png' class='img_first'>$travel_title</a></td>
<td><img src='comment.png' class='img_second'><p>$travel_comments_count</p></td>
<td><a href='profile_user.php?id=".$user_id."'><img src='avatar.png' class='img_third'>$username</a></td>
<td><p><img src='views.png' class='img_four'>$travel_views</p></td>
<td><p>$travel_subject_date_format</p></td>
</tr>";
}
}
for ($i = 1; $i <= $number_of_pages; $i++) {
$pagination .= "<a href='forum_index.php?page=".$i."&limit=".$offset."'>$i</a>";
}
$data = array('response_message' => $response_message, 'pagination' => $pagination);
echo json_encode($data);
exit();
}
}
注意:变量$page
始终为1,因此似乎无法设置$_GET['page']
,因此,下面的页面始终显示前10个结果。为什么?
这是php文件,我想显示结果:
<div class='display-subjects'>
<table class='display-subjects-table' cols='5'>
<tbody id="topicArea">
</tbody>
</table>
</div>
<div id='pagination'>
<?php
if(isset($_GET['page'])) {
$page = $_GET['page'];
}
else {
$page = 1;
}
?>
</div>
错误的部分在哪里?提前谢谢。
答案 0 :(得分:1)
displayAllTopics()
需要将页码作为参数,然后在data
选项中传递。
function displayAllTopics(pageNum) {
$.ajax({
url: "requests.php",
type: "GET",
data: {
keyPagination: "displayAllTopics",
page: pageNum
},
dataType: "json",
success: function(response) {
$("#topicArea").append(response.response_message);
$("#pagination").append(response.pagination);
}
});
}