Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
7.6 kB
20
Indexable
Never
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="../styles/styles.css">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <title>RITS | Item data</title>
    <style>
        .content-container {
            margin-left: 290px;
        }

        @media (max-width: 1000px) {
            .content-container {
                padding-left: 0;
                margin: 0;
            }
        }
    </style>

</head>

<body class="content-container">
    <?php
    include '../templates/_navbar.php';
    include '../templates/sidebar.php';
    include('../db/db_connection.php');
    $itemsPerPage = 8;

    if (isset($_GET['page']) && is_numeric($_GET['page'])) {
        $currentPage = intval($_GET['page']);
    } else {
        $currentPage = 1;
    }

    $offset = ($currentPage - 1) * $itemsPerPage;

    if (isset($_GET['search'])) {
        $search = $_GET['search'];
        $search = str_replace("'", '', $search);

        $strsql = "SELECT * FROM Tbl_Customer 
               WHERE Fname LIKE '%$search%' 
               OR Lname LIKE '%$search%' 
               OR gndr LIKE '%$search%' 
               OR Address LIKE '%$search%'
               OR ntlty LIKE'%$search%'
               ORDER BY AcctId OFFSET $offset ROWS FETCH NEXT $itemsPerPage ROWS ONLY";
    } else {
        $strsql = "SELECT * FROM Tbl_Customer ORDER BY AcctId OFFSET $offset ROWS FETCH NEXT $itemsPerPage ROWS ONLY";
    }

    $query = sqlsrv_query($conn, $strsql);

    if (!$query) {
        die("Query failed: " . sqlsrv_errors());
    }

    $totalItemsQuery = sqlsrv_query($conn, "SELECT COUNT(*) as total FROM Tbl_Customer");
    $totalItems = sqlsrv_fetch_array($totalItemsQuery, SQLSRV_FETCH_ASSOC)['total'];
    $totalPages = ceil($totalItems / $itemsPerPage);

    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_id'])) {
        $deleteId = $_POST['delete_id'];
        $deleteSql = "DELETE FROM Tbl_Customer WHERE AcctId = ?";
        $deleteParams = array($deleteId);
        $deleteQuery = sqlsrv_query($conn, $deleteSql, $deleteParams);

        if ($deleteQuery) {
            echo '<script>
                    Swal.fire({
                        title: "Success!",
                        text: "Data deleted successfully.",
                        icon: "success",
                        showConfirmButton: false,
                        timer: 1500
                    }).then(() => {

                    });
                  </script>';
        } else {
            echo '<script>
                    Swal.fire({
                        title: "Error!",
                        text: "Error deleting data.",
                        icon: "error",
                        showConfirmButton: true,
                    });
                  </script>';
        }
    }
    ?>

    <div>
        <div style="margin-left: 20px; margin-right: 20px; display: flex; justify-content: space-between; align-items: center;">
            <h4 href="#" style="width: 150px; margin: 0;">Customers</h4>
            <div class="right" style="display: flex; align-items: center;">
                <form action="../pages/customers.php" method="get" style="display: flex; align-items: center;">
                    <input type="text" name="search" id="searchBox" placeholder="Search..." style="width: 300px; margin-right: 10px;" oninput="validateInput(this);">
                    <button class="waves-effect green-text btn-flat" type="submit">
                        <i class="material-icons">search</i>
                    </button>
                    <button class="waves-effect green-text btn-flat" type="button" onclick="refreshPage()">
                        <i class="material-icons">refresh</i>
                    </button>
                </form>
                <a class="waves-effect green darken-4 btn-large" href="../pages/itemAdd.php" style="margin: 0;">Add Items</a>
            </div>
        </div>
        <div class="row">
            <div class="col s12">
                <table class="striped highlight responsive-table">
                    <thead>
                        <tr>
                            <th>Account #</th>
                            <th>First name</th>
                            <th>Last name</th>
                            <th>Gender</th>
                            <th>Address</th>
                            <th>Nationality</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) { ?>
                            <tr>
                                <td><?= $row['AcctId'] ?></td>
                                <td><?= $row['Fname'] ?></td>
                                <td><?= $row['Lname'] ?></td>
                                <td><?= $row['gndr'] ?></td>
                                <td><?= $row['Address'] ?></td>
                                <td><?= $row['ntlty'] ?></td>
                                <td class="right">
                                    <a class="btn waves-effect" href="../pages/customeredit.php?myid=<?= $row['AcctId'] ?>">Edit</a>
                                    <form method="post" action="" style="display: inline;">
                                        <input type="hidden" name="delete_id" value="<?= $row['AcctId'] ?>">
                                        <button type="submit" class="btn red darken-3 white-text waves-effect">Delete</button>
                                    </form>
                                </td>
                            </tr>
                        <?php } ?>
                    </tbody>
                </table>
                <?php
                sqlsrv_close($conn);
                ?>
            </div>
        </div>

        <div class="row">
            <ul class="pagination center">
                <?php
                if ($totalPages > 1) {
                    if ($currentPage > 1) {
                        echo "<li class='waves-effect'><a href='../pages/customers.php?page=" . ($currentPage - 1) . "'><i class='material-icons'>chevron_left</i></a></li>";
                    }
                    for ($i = 1; $i <= $totalPages; $i++) {
                        $activeClass = ($i === $currentPage) ? 'active' : '';
                        echo "<li class='waves-effect $activeClass'><a href='../pages/customers.php?page=$i'>$i</a></li>";
                    }
                    if ($currentPage < $totalPages) {
                        echo "<li class='waves-effect'><a href='../pages/customers.php?page=" . ($currentPage + 1) . "'><i class='material-icons'>chevron_right</i></a></li>";
                    }
                }
                ?>
            </ul>
        </div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
    <script>
        function refreshPage() {
            window.location.href = '../pages/items.php';
        }

        function validateInput(input) {
            input.value = input.value.replace(/'/g, '');
        }
    </script>
</body>

</html>