Ajax Load More Pagination in PHP

 <?php


$servername = "localhost";
$username = "root";
$password = "";
$database = "test";

//Create Connection
$conn = new mysqli($servername, $username, $password, $database);

//Check Connection
if ($conn->connect_error) {
    die("Connection Failed : " . $conn->connect_error);
}
Above is mysqli_conn.php File which is included in below Files.







Below is index.php File.
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>PHP Ajax Load More Pagination</title>
    <link rel="stylesheet" href="css/style.css">
</head>

<body>
    <div id="main">
        <div id="header">
            <h1>PHP & Ajax Load More Pagination</h1>
        </div>

        <div id="table-data">
            <table id="loadData" border="1" width="100%" cellspacing="0" cellpadding="10px">
                <tr>
                    <th>Id</th>
                    <th>Student Name</th>
                    <th>Age</th>
                    <th>City</th>
                </tr>
            </table>
        </div>
    </div>
    <script type="text/javascript" src="js/jquery-3.7.1.min.js"></script>
    <script>
        $(document).ready(function() {
            function loadTable(page) {
                $.ajax({
                    url: "ajax-pagination.php",
                    type: "POST",
                    data: {
                        page_no: page
                    },
                    success: function(data) {
                        if (data) {
                            $("#pagination").remove();
                            $("#loadData").append(data);
                        } else {
                            $("#ajaxbtn").html("Finished");
                            $("#ajaxbtn").prop("disabled", true);
                        }
                    }
                });
            }
            loadTable();

            $(document).on("click", "#ajaxbtn", function() {
                $("#ajaxbtn").html("Loading...");
                var pid = $(this).data("id");
                loadTable(pid);
            });
        });
    </script>
</body>

</html>







Below is ajax-pagination.php File.
<?php

include("mysqli_conn.php");

$limit = 5;
if (isset($_POST["page_no"])) {
    $page = $_POST["page_no"];
} else {
    $page = 0;
}

$sql = "SELECT * FROM students LIMIT {$page},{$limit}";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $output = "";
    $output .= "<tbody>";

    while ($row = $result->fetch_assoc()) {
        $last_id = $row["id"];
        $output .= "<tr>
                        <td align='center'>{$row['id']}</td>
                        <td>{$row['student_name']}</td>
                        <td>{$row['age']}</td>
                        <td>{$row['city']}</td>
                    </tr>";
    }

    $output .= "</tbody>

                <tbody id='pagination'>
                    <tr>
                        <td colspan='4'>
                            <button id='ajaxbtn' data-id='{$last_id}'>Load More</button>
                        </td>
                    </tr>
                </tbody>";
    echo $output;
} else {
    echo "";
}
$conn->close();








Below is css/style.css File.
body {
    font-family: arial;
    background: #b2bec3;
    padding: 0;
    margin: 0;
}

h1 {
    text-align: center;
    margin: 0;
}

#main {
    width: 700px;
    margin: 0 auto;
    background: #fff;
    font-size: 19px;
}

#header {
    background: #6c5ce7;
    color: #ffffff;
    padding: 15px;
}

#table-data {
    padding: 15px;
    min-height: 500px;
}

#table-data th {
    background: #e84393;
    color: #fff;
}

#table-data tbody tr:nth-child(even) {
    background: #ecf0f1;
}

#pagination {
    text-align: center;
}

#pagination tr {
    background: #fff;
}

#ajaxbtn {
    background: #6c5ce7;
    color: #fff;
    border: 0;
    outline: 0;
    font-size: 18px;
    padding: 10px 100px;
    border-radius: 4px;
    cursor: pointer;
}

#ajaxbtn:disabled {
    background: #7f8c8d;
    color: #ffffff;
    cursor: default;
}






Comments

Popular posts from this blog

Logical_Operators

SubQuery with EXISTS and NOT EXISTS in PHP

Get Functions