我有 15 个帖子(在我的案例中是提示),我从数据库中获得,当用户点击其中一个时,他通过给 id 和 url 被发送到该帖子的详细信息页面。
例子: 在页面顶部:
$imagesToApprove = Prompt::get15ToApproveImages();
在html中:
<?php foreach ($imagesToApprove as $imageToApprove) : ?>
<a href="promptDetails.php?id=<?php echo $imageToApprove['id'] ?>">
<img src="<?php echo $imageToApprove['cover_url']; ?>" alt="prompt">
</a>
<?php endforeach; ?>
现在我想对这个无限滚动做同样的事情,当用户向下滚动新帖子时,我会从数据库中加载新帖子,我用 AJAX 和 PHP 做了这个,但我似乎无法弄清楚如何给 id有了它。
html:
<main class="flex flex-wrap">
<div id="image-container" class="flex flex-wrap"></div>
</main>
<script src="ajax/infiniteScrollToApprove.js"></script>
AJAX 文件 infiniteScrollToApprove.js:
var offset = 0;
var limit = 20;
// Initialize loading flag
var loading = false;
function loadImages() {
// Set loading flag to true to prevent multiple requests from being made simultaneously
loading = true;
// Create new XMLHttpRequest object
var xhr = new XMLHttpRequest();
// Set request method, URL and headers
xhr.open("POST", "./loadPromptsToApprove.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// Handle response from server
xhr.onload = function () {
if (xhr.status === 200) {
// Parse JSON response
var images = JSON.parse(xhr.responseText);
console.log("Parsed images:", images);
// Loop through images and create img elements
if (images.length > 0) {
for (var i = 0; i < images.length; i++) {
var img = document.createElement("img");
img.setAttribute("src", images[i].cover_url);
img.classList.add("flex", "w-1/4");
document.getElementById("image-container").appendChild(img);
}
// Update offset to keep track of number of images loaded
offset += limit;
// Set loading flag to false to allow new requests to be made
loading = false;
} else {
// If there are no more images to load, remove scroll event listener to prevent further requests
window.removeEventListener("scroll", checkScroll);
}
}
};
// Send AJAX request with offset and limit parameters
xhr.send("offset=" + offset + "&limit=" + limit);
}
// Function to check if user has scrolled to bottom of page
function checkScroll() {
if (
window.scrollY + window.innerHeight >=
document.documentElement.scrollHeight
) {
// If user has scrolled to bottom of page and no requests are currently loading, call loadImages() function to fetch new images
if (!loading) {
loadImages();
}
}
}
// Attach scroll event listener to window object to trigger checkScroll() function when user scrolls
window.addEventListener("scroll", checkScroll);
// Load initial images
loadImages();
loadToApprove提示:
<?php
include_once("bootstrap.php");
// Start output buffering
ob_start();
// Retrieve offset and limit values from POST request
$offset = $_POST['offset'];
$limit = $_POST['limit'];
// Retrieve array of images from database using the offset and limit values
$images = Prompt::getAllToApproveImages($offset, $limit);
// Encode the array of images as a JSON object and send it back as the response to the AJAX request
echo json_encode($images);
// Flush the output buffer
ob_end_flush();