<?php

// Database connection

$host = 'localhost';

$db = 'demp_p';

$user = 'root';

$pass = 'root';


$conn = new mysqli($host, $user, $pass, $db);


if ($conn->connect_error) {

    die("Connection failed: " . $conn->connect_error);

}


// Configuration

$limit = 5; // Number of records per page

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

$start = ($page > 1) ? ($page * $limit) - $limit : 0;


// Count total records

// $totalQuery = $conn->query("SELECT COUNT(*) as count FROM items");

// print_r($totalQuery->fetch_assoc(MYSQLI_ASSOC));die;

$total = 100;

// $total = $totalQuery->fetch_assoc()['count'];

$pages = ceil($total / $limit);


// Fetch records

// $query = $conn->prepare("SELECT * FROM items LIMIT ?, ?");

// $query->bind_param("ii", $start, $limit);

// $query->execute();

// $result = $query->get_result();

// $items = $result->fetch_all(MYSQLI_ASSOC);

?>


<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Pagination Example</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<div class="container mt-5">

    <h2>Items List</h2>

    <ul class="list-group mb-3">

        <?php foreach ($items as $item): ?>

            <li class="list-group-item"><?= htmlspecialchars($item['name']) ?></li>

        <?php endforeach; ?>

    </ul>


    <nav aria-label="Page navigation">

        <ul class="pagination justify-content-center">

            <!-- Previous Button -->

            <li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">

                <a class="page-link" href="?page=<?= $page - 1 ?>" aria-label="Previous">

                    <span aria-hidden="true">&laquo;</span>

                </a>

            </li>


            <!-- Page Numbers -->

            <?php

            $range = 4; // Adjust this value to show more pages around the current page

            $startPage = max(1, $page - $range);

            $endPage = min($pages, $page + $range);


            if ($startPage > 1) {

                echo '<li class="page-item"><a class="page-link" href="?page=1">1</a></li>';

                if ($startPage > 4) {

                    echo '<li class="page-item disabled"><a class="page-link">...</a></li>';

                }

            }


            for ($i = $startPage; $i <= $endPage; $i++) {

                echo '<li class="page-item ' . (($i == $page) ? 'active' : '') . '">';

                echo '<a class="page-link" href="?page=' . $i . '">' . $i . '</a>';

                echo '</li>';

            }


            if ($endPage < $pages) {

                if ($endPage < $pages - 1) {

                    echo '<li class="page-item disabled"><a class="page-link">...</a></li>';

                }

                echo '<li class="page-item"><a class="page-link" href="?page=' . $pages . '">' . $pages . '</a></li>';

            }

            ?>


            <!-- Next Button -->

            <li class="page-item <?= ($page >= $pages) ? 'disabled' : '' ?>">

                <a class="page-link" href="?page=<?= $page + 1 ?>" aria-label="Next">

                    <span aria-hidden="true">&raquo;</span>

                </a>

            </li>

        </ul>

    </nav>

</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

</body>

</html>


<?php

// Close the database connection

$conn->close();

?>


Comments

Popular posts from this blog

How to Delete record using PHP Ajax

How to seperate character from string in php

Uploads Only 10 files in month step by step