Viewing file: slider.php (5.37 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<!DOCTYPE html> <html lang="en">
<?php include 'head.php';?> <style> table { border-collapse: collapse; width: 100%; margin-top: 20px; }
table, th, td { border: 1px solid #ddd; }
th, td { padding: 8px; text-align: left; }
th { background-color: #f2f2f2; font-weight: bold; } .custom-image { width: 100px; height: 100px; } </style> <body>
<!-- ======= Header ======= --> <?php include 'menubar.php';?>
<?php include 'sidebar.php';?>
<main id="main" class="main"> <?php include 'connect.php';
// Upload image if (isset($_POST['add'])) { if ($_FILES['slider_photo']['name']) { $filename = basename($_FILES['slider_photo']['name']); $target = "slider/" . $filename; if (move_uploaded_file($_FILES['slider_photo']['tmp_name'], $target)) { mysqli_query($link, "INSERT INTO slider_photo (slider_photo) VALUES ('$filename')"); echo "<script>alert('Image added');location.href='slider.php';</script>"; } else { echo "<script>alert('Upload failed');</script>"; } } }
// Delete image if (isset($_GET['delete'])) { $id = $_GET['delete']; $result = mysqli_query($link, "SELECT slider_photo FROM slider_photo WHERE ID=$id"); $row = mysqli_fetch_assoc($result); if ($row) { unlink("slider/" . $row['slider_photo']); // delete file mysqli_query($link, "DELETE FROM slider_photo WHERE ID=$id"); } echo "<script>alert('Deleted');location.href='slider.php';</script>"; }
// Update image if (isset($_POST['update'])) { $id = $_POST['edit_id']; if ($_FILES['new_photo']['name']) { $filename = basename($_FILES['new_photo']['name']); $target = "slider/" . $filename; if (move_uploaded_file($_FILES['new_photo']['tmp_name'], $target)) { // Delete old photo $old = mysqli_fetch_assoc(mysqli_query($link, "SELECT slider_photo FROM slider_photo WHERE ID=$id")); unlink("slider/" . $old['slider_photo']); // Update DB mysqli_query($link, "UPDATE slider_photo SET slider_photo='$filename' WHERE ID=$id"); echo "<script>alert('Updated');location.href='slider.php';</script>"; } } } ?>
<!DOCTYPE html> <html> <head> <title>Slider Admin</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"> <style> .img-preview { max-width: 100%; height: 200px; object-fit: cover; } </style> </head> <body class="bg-light"> <div class="container py-5">
<h2 class="mb-4 text-center">Slider Photo Manager</h2>
<!-- Add Form --> <form method="POST" enctype="multipart/form-data" class="card p-3 mb-5 shadow-sm"> <h5>Add New Slider Image</h5> <div class="mb-3"> <input type="file" name="slider_photo" class="form-control" required> </div> <button type="submit" name="add" class="btn btn-success">Upload</button> </form>
<!-- List of Slider Images --> <div class="row g-4"> <?php $result = mysqli_query($link, "SELECT * FROM slider_photo ORDER BY ID DESC"); while ($row = mysqli_fetch_assoc($result)) { ?> <div class="col-md-4"> <div class="card shadow-sm"> <img src="slider/<?php echo $row['slider_photo']; ?>" class="img-preview card-img-top"> <div class="card-body"> <p><strong>ID:</strong> <?php echo $row['ID']; ?></p>
<!-- Edit Button triggers Modal --> <button class="btn btn-warning btn-sm" data-bs-toggle="modal" data-bs-target="#editModal<?php echo $row['ID']; ?>">Edit</button>
<!-- Delete --> <a href="?delete=<?php echo $row['ID']; ?>" onclick="return confirm('Are you sure?')" class="btn btn-danger btn-sm">Delete</a> </div> </div> </div>
<!-- Edit Modal --> <div class="modal fade" id="editModal<?php echo $row['ID']; ?>" tabindex="-1"> <div class="modal-dialog"> <form method="POST" enctype="multipart/form-data" class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Edit Image ID: <?php echo $row['ID']; ?></h5> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <div class="modal-body"> <input type="hidden" name="edit_id" value="<?php echo $row['ID']; ?>"> <input type="file" name="new_photo" class="form-control" required> </div> <div class="modal-footer"> <button type="submit" name="update" class="btn btn-primary">Update</button> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button> </div> </form> </div> </div>
<?php } ?> </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
</main><!-- End #main -->
<?php include 'footer.php';?>
</body>
</html>
|