Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>PHP Pagination</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
- </head>
- <body>
- <?php
- // Import the file where we defined the connection to Database.
- require_once "./db-conn.php";
- $limit = 10; // Number of entries to show in a page.
- // Look for a GET variable page if not found default is 1.
- if (isset($_GET["page"])) {
- $pn = $_GET["page"];
- } else {
- $pn = 1;
- };
- $start_from = ($pn - 1) * $limit;
- $sql = "SELECT * FROM apps_countries LIMIT $start_from, $limit";
- $rs_result = $conn->query($sql);
- ?>
- <div class="container">
- <br>
- <div>
- <h1>PHP Pagination Using MySQLi</h1>
- <p>This page is just for demonstration of Basic Pagination using PHP.</p>
- <table class="table table-striped table-condensed table-bordered">
- <thead>
- <tr>
- <th width="10%">Code</th>
- <th>Country Name</th>
- </tr>
- </thead>
- <tbody>
- <?php
- while ($row = $rs_result->fetch_assoc()) {
- // Display each field of the records.
- ?>
- <tr>
- <td><?php echo $row["country_code"]; ?></td>
- <td><?php echo $row["country_name"]; ?></td>
- </tr>
- <?php
- };
- ?>
- </tbody>
- </table>
- <ul class="pagination">
- <?php
- $sql = "SELECT COUNT(*) FROM apps_countries";
- $rs_result = $conn->query($sql);
- $row = mysqli_fetch_row($rs_result);
- $total_records = $row[0];
- // Number of pages required.
- $total_pages = ceil($total_records / $limit);
- $pagLink = "";
- for ($i = 1; $i <= $total_pages; $i++) {
- if ($i == $pn) {
- $pagLink .= "<li class='active'><a href='index.php?page="
- . $i . "'>" . $i . "</a></li>";
- } else {
- $pagLink .= "<li><a href='index.php?page=" . $i . "'>
- " . $i . "</a></li>";
- }
- };
- echo $pagLink;
- ?>
- </ul>
- </div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement