Jump to content

I can't dynamically paginate with this php script

Go to solution Solved by mac_gyver,

Recommended Posts

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);
$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


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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.