$page = 1; // set the current page number $per_page = 10; // set the number of records per page $start = ($page - 1) * $per_page; // calculate the starting record // create an SQL query to fetch records with limit and offset $query = "SELECT * FROM `table_name` LIMIT $per_page OFFSET $start"; // execute the query and retrieve the records $result = mysqli_query($conn, $query);
// load the pagination class require_once 'pagination.php'; // set the current page number and number of records per page $page = isset($_GET['page']) ? $_GET['page'] : 1; $per_page = 10; // create an instance of the pagination class $pagination = new Pagination($page, $per_page); // fetch the total number of records $total_records = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) AS count FROM `table_name`"))['count']; // set the total number of records in the pagination object $pagination->setTotal($total_records); // create the pagination links $links = $pagination->createLinks();In this example, we use a pagination class to create pagination links that allow users to navigate through the pages. The class encapsulates the logic for calculating the starting record position and creating the pagination links. We retrieve the total number of records from the database using an SQL count query and pass it to the pagination object. Finally, we call the `createLinks` method to generate the pagination links. Package Library: This code example uses a pagination class. The pagination class can be part of a PHP package library or a standalone class, depending on the implementation.