PHP Classes

Query interaction with pagination class

Recommend this page to a friend!

      PHP Pagination Library  >  All threads  >  Query interaction with pagination class  >  (Un) Subscribe thread alerts  
Subject:Query interaction with pagination class
Summary:Query interaction with pagination class
Messages:2
Author:António Lourenço
Date:2022-09-02 15:58:54
 

 


  1. Query interaction with pagination class   Reply   Report abuse  
Picture of António Lourenço António Lourenço - 2022-09-02 15:58:54
How can I integrate this class with a database query?

  2. Re: Query interaction with pagination class   Reply   Report abuse  
Picture of Kemal GENIS Kemal GENIS - 2025-03-20 07:50:55 - In reply to message 1 from António Lourenço
<?php
// Number of rows to display per page
$pagePerRow = 10;

// Get the current page number from the URL query string (default to 1 if not set)
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;

// Connect to the MySQL database
$db = mysqli_connect("localhost", "dbuser", "dbpass123", "db_test");

// SQL query to select all rows from the 'users' table
$sql = "SELECT * FROM users";

// Execute the query to get the total number of rows
$totalQuery = mysqli_query($db, $sql); // Corrected: Added $db as the first parameter
$totalRowCount = mysqli_num_rows($totalQuery);

// Calculate the LIMIT clause for pagination
// (page - 1) * pagePerRow gives the starting row, and pagePerRow is the number of rows to fetch
$limitSql = $sql . " LIMIT " . (($page - 1) * $pagePerRow) . ", " . $pagePerRow;

// Execute the query with the LIMIT clause
$limitQuery = mysqli_query($db, $limitSql); // Corrected: Added $db as the first parameter
$limitRows = mysqli_fetch_assoc($limitQuery);

// Calculate the total number of pages needed for pagination
$totalPageCount = ceil($totalRowCount / $pagePerRow); // Corrected: Added $ before pagePerRow

// Include the pagination class file
include_once "pagination.class.php";

// Create an instance of the Pagi class, passing the total page count and the query parameter for pagination
$pagi = new Pagi($totalPageCount, 'page');

// Output the pagination links
echo $pagi->getContent();
?>