All Tutorials

Your One-Stop Destination for Learning and Growth

Paging with PHP and MySQL

Paging is an essential functionality for displaying large result sets in web applications. In this blog post, we will discuss how to implement paging using PHP and MySQL.

Prerequisites

Before we begin, make sure you have a good understanding of the following:

  • Basic PHP programming
  • MySQL database querying

Setting up the Environment

Let's assume we are building a simple blog application. We will create a table named posts in our MySQL database to store blog posts data. The table schema would look like this:

CREATE TABLE posts (
    id INT PRIMARY KEY AUTO_INCREMENT,
    title VARCHAR(255) NOT NULL,
    content TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE (id)
);

Implementing the Paging Logic

To implement paging, we will create two files: index.php and paging.php. Let's start by creating the paging.php file to handle our paging logic.

<?php
$limit = 5; // Number of records per page
$page = isset($_GET['page']) ? $_GET['page'] : 1; // Current page number
$offset = ($page - 1) * $limit; // Previous record offset

// Fetch data from the database using the provided offset and limit
$query = "SELECT id, title FROM posts LIMIT $limit OFFSET $offset";
$result = mysqli_query(connection(), $query);

// Get total number of records in the table
$query = "SELECT COUNT(*) as num FROM posts";
$totalRecords = mysqli_fetch_assoc(mysqli_query(connection(), $query))['num'];

// Calculate total number of pages
$totalPages = ceil($totalRecords / $limit);
?>

In the code above, we set the limit to 5 records per page and fetch the current page number from the query string. We then calculate the offset based on the current page number and limit. The rest of the code is used to fetch data from the database using the provided offset and limit, as well as get the total number of records in the table and calculate the total number of pages.

Creating the index.php file

Now let's create the index.php file that will display our paged result set:

<?php include 'paging.php'; ?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Blog</title>
</head>
<body>
    <?php if ($page > 1): ?>
        <a href="index.php?page=<?php echo $page - 1; ?>">&laquo;</a>
    <?php endif; ?>

    <ul>
        <?php while ($row = mysqli_fetch_assoc($result)): ?>
            <li><a href="post.php?id=<?php echo $row['id']; ?>"><?php echo htmlspecialchars($row['title']); ?></a></li>
        <?php endwhile; ?>
    </ul>

    <?php if ($page < $totalPages): ?>
        <a href="index.php?page=<?php echo $page + 1; ?>">&raquo;</a>
    <?php endif; ?>
</body>
</html>

In the code above, we include the paging.php file to get the paging logic and data. We then create a simple HTML structure to display our blog posts, including the previous and next page links.

Conclusion

In this post, we discussed how to implement paging using PHP and MySQL. This technique can be applied to various web applications where you need to display large result sets in smaller chunks. We hope you find this information helpful and inspire you to build your own dynamic web applications!

Published April, 2014