CSS-Regex Posted February 18 Share Posted February 18 try { $postData = json_decode(file_get_contents("php://input"), true); $limit_page = 100; $page = $postData['page'] ?? 1; $offset = ($page - 1) * $limit_page; echo $offset; // Retrieve the total number of rows from the database $totalRows = $dbconn->query("SELECT COUNT(*) FROM photos")->fetchColumn(); // Calculate the total number of pages $totalPages = ceil($totalRows / $limit_page); // Fetch data for the current page $stmt = $dbconn->prepare("SELECT * FROM photos LIMIT :offset,:limit_page"); $stmt->bindParam(':limit_page', $limit_page, PDO::PARAM_INT); $stmt->bindParam(':offset', $offset, PDO::PARAM_INT); $stmt->execute(); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); // Return both data for the current page and the total number of pages $response = ['data' => $results, 'totalPages' => $totalPages]; echo json_encode($response); } catch (PDOException $e) { echo "Error: " . $e->getMessage(); exit(); // Exit the script if an error occurs } I have tried to hard code the xhr.send with an example like this xhr.send(JSON.stringify(2)); I've increased it from 1 to 2, but the display in the console is still showing up with the first batch of 100 results, no matter what I do Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted February 18 Solution Share Posted February 18 you would need to use JSON.stringify({page:2}) for there to be a 'page' element in the decoded data. Quote Link to comment Share on other sites More sharing options...
CSS-Regex Posted February 18 Author Share Posted February 18 (edited) Wow, it now makes the pagination work dynamically Edited February 18 by CSS-Regex Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.