Lesson 80 of 158 – API Pagination
80%

API Pagination

API pagination is used to divide a large number of records into smaller pages. Instead of sending thousands of records in one API response, the server sends a limited number of records at a time.

Note: Pagination improves API performance, reduces response size, and makes large lists easier to display in React Native applications.

1. What is API Pagination?

Pagination means dividing a large collection of records into multiple smaller pages.

1000 Records
     ↓
Page 1 → 20 Records
Page 2 → 20 Records
Page 3 → 20 Records
...
Page 50 → 20 Records

2. Why Do We Need Pagination?

Suppose a student database contains 10,000 students. Returning all 10,000 students in a single API response can consume unnecessary network bandwidth and memory.

Pagination allows the application to request only a small number of students at a time.

3. Page Parameter

A common pagination API uses a page query parameter.

GET /api/students.php?page=1

The value 1 means that the client wants the first page.

4. Limit Parameter

The limit parameter defines how many records should be returned per page.

GET /api/students.php?page=1&limit=20

This requests page 1 with up to 20 records.

5. OFFSET in SQL

MySQL uses LIMIT and OFFSET for a common pagination technique.

SELECT *
FROM students
LIMIT 20 OFFSET 0;

This returns the first 20 records.

6. Understanding LIMIT

LIMIT tells MySQL how many records to return.

LIMIT 10

This means that at most 10 records should be returned.

SELECT *
FROM students
LIMIT 10;

7. Understanding OFFSET

OFFSET tells MySQL how many records should be skipped before returning records.

LIMIT 10 OFFSET 20

This skips the first 20 records and then returns the next 10 records.

8. Page 1 Calculation

Suppose each page contains 10 records.

Page = 1
Limit = 10

Offset =
(page - 1) × limit

Offset =
(1 - 1) × 10

Offset = 0

Therefore:

LIMIT 10 OFFSET 0

9. Page 2 Calculation

Page = 2
Limit = 10

Offset =
(2 - 1) × 10

Offset = 10

Therefore:

LIMIT 10 OFFSET 10

The API skips the first 10 records and returns the next 10.

10. Page 3 Calculation

Page = 3
Limit = 10

Offset =
(3 - 1) × 10

Offset = 20

Therefore:

LIMIT 10 OFFSET 20

11. Pagination Formula

The basic pagination formula is:

offset = (page - 1) × limit

This formula converts a page number into the database offset.

12. Read Page and Limit in PHP

$page = (int)(
    $_GET['page'] ?? 1
);

$limit = (int)(
    $_GET['limit'] ?? 10
);

Casting the values to integers is useful when working with numeric pagination parameters.

13. Validate Page Number

A page number should not be less than 1.

Page 0  → Page 1
Page -1 → Page 1
Page 1  → Page 1
Page 2  → Page 2

14. Validate Limit

It is a good idea to restrict the maximum number of records that a client can request.

$limit = max(
    1,
    min($limit, 100)
);

Here the API allows between 1 and 100 records per page.

15. Calculate Offset in PHP

$offset =
    ($page - 1) * $limit;

For example:

Page 1, Limit 20
Offset = 0

Page 2, Limit 20
Offset = 20

Page 3, Limit 20
Offset = 40

16. Complete PHP Pagination API

<?php

header(
    "Content-Type: application/json"
);

require_once '../db.php';

$page = (int)(
    $_GET['page'] ?? 1
);

$limit = (int)(
    $_GET['limit'] ?? 10
);

$page = max(
    1,
    $page
);

$limit = max(
    1,
    min($limit, 100)
);

$offset =
    ($page - 1) * $limit;

try {

    $sql = "
        SELECT
            id,
            student_id,
            name,
            course,
            status
        FROM students
        ORDER BY id DESC
        LIMIT ? OFFSET ?
    ";

    $stmt = $pdo->prepare($sql);

    $stmt->bindValue(
        1,
        $limit,
        PDO::PARAM_INT
    );

    $stmt->bindValue(
        2,
        $offset,
        PDO::PARAM_INT
    );

    $stmt->execute();

    $students =
        $stmt->fetchAll(
            PDO::FETCH_ASSOC
        );

    echo json_encode([
        "success" => true,
        "page" => $page,
        "limit" => $limit,
        "data" => $students
    ]);

} catch (PDOException $e) {

    http_response_code(500);

    echo json_encode([
        "success" => false,
        "message" =>
            "Server error"
    ]);
}

?>

17. Why Use bindValue() for LIMIT?

When using PDO with MySQL, explicitly binding pagination values as integers makes their intended type clear.

$stmt->bindValue(
    1,
    $limit,
    PDO::PARAM_INT
);

$stmt->bindValue(
    2,
    $offset,
    PDO::PARAM_INT
);

This is especially useful for LIMIT and OFFSET values.

18. Return Pagination Information

A useful API response can include information about the current page.

{
    "success": true,
    "page": 2,
    "limit": 20,
    "data": []
}

The mobile application can use this information to manage pagination.

19. Total Record Count

The API may also return the total number of records.

SELECT COUNT(*)
FROM students;

Using the total count, the application can calculate how many pages are available.

20. Calculate Total Pages

The total number of pages can be calculated using:

$totalPages = ceil(
    $totalRecords / $limit
);

For example:

Total Records = 95
Limit = 10

Total Pages =
ceil(95 / 10)

Total Pages = 10

21. Better Pagination Response

{
    "success": true,
    "page": 2,
    "limit": 10,
    "total_records": 95,
    "total_pages": 10,
    "data": [
        {
            "id": 20,
            "name": "Rahul"
        }
    ]
}

This response gives the mobile application enough information to display pagination controls.

22. React Native Pagination Request

const page = 1;
const limit = 20;

const response = await fetch(
    "https://example.com/api/students.php"
    + "?page=" + page
    + "&limit=" + limit
);

const result =
    await response.json();

console.log(result.data);

23. React Native Page State

const [page, setPage] =
    useState(1);

const [students, setStudents] =
    useState([]);

const [loading, setLoading] =
    useState(false);

The page state can be changed whenever the user requests another page.

24. Load More Records

A common mobile application pattern is to load another page when the user reaches the end of the current list.

const loadNextPage = () => {

    setPage(
        previousPage =>
            previousPage + 1
    );

};

The application can then request the next page from the API.

25. FlatList onEndReached

React Native FlatList provides onEndReached, which can be used to load additional pages.

<FlatList
    data={students}
    keyExtractor={(item) =>
        item.id.toString()
    }
    renderItem={({ item }) => (
        <Text>
            {item.name}
        </Text>
    )}
    onEndReached={loadNextPage}
    onEndReachedThreshold={0.5}
/>

26. Avoid Duplicate API Requests

When using infinite scrolling, the application should prevent multiple requests from being sent at the same time.

if (loading) {
    return;
}

setLoading(true);

After the request finishes:

setLoading(false);

27. Pagination with Sorting

Pagination should normally be combined with a consistent sort order.

SELECT *
FROM students
ORDER BY id DESC
LIMIT ? OFFSET ?;

A stable ordering helps prevent unexpected changes between pages.

28. Pagination with Filtering

Filtering and pagination can also be combined.

GET /api/students.php
?status=active
&page=1
&limit=20

The database query can apply the filter before returning the requested page.

SELECT *
FROM students
WHERE status = ?
ORDER BY id DESC
LIMIT ? OFFSET ?;

29. Test Pagination in Postman

Method: GET

http://localhost/api/students.php
?page=1&limit=10

Test page 2:

http://localhost/api/students.php
?page=2&limit=10

Test page 3:

http://localhost/api/students.php
?page=3&limit=10

Compare the returned records to understand how pagination works.

30. API Pagination Summary

API pagination divides a large collection into smaller pages. PHP receives the page and limit values, calculates the offset, and uses MySQL LIMIT and OFFSET to retrieve the required records. React Native can then display the data page by page or use infinite scrolling.

Page 1
 ↓
LIMIT 20 OFFSET 0

Page 2
 ↓
LIMIT 20 OFFSET 20

Page 3
 ↓
LIMIT 20 OFFSET 40

Pagination becomes especially useful when combined with search, filtering, sorting, and a React Native FlatList.

📌 Key Points

  • Pagination divides a large API result into smaller pages.
  • The page parameter identifies the requested page.
  • The limit parameter controls records per page.
  • MySQL uses LIMIT and OFFSET for common pagination.
  • The offset formula is (page - 1) × limit.
  • Page numbers should start from 1.
  • The API should limit the maximum page size.
  • Total records and total pages can be included in the JSON response.
  • React Native can manage the current page using state.
  • FlatList onEndReached can support load-more functionality.
  • Duplicate pagination requests should be prevented.
  • Pagination should use a consistent sort order.
  • Pagination can be combined with search and filtering.
  • The next lesson will cover multiple query parameters.

🧠 Quick Quiz

Question: Which SQL keywords are commonly used for API pagination in MySQL?