$tmp_name) { $file_name = basename($_FILES['multiple_images']['name'][$key]); $target_dir = "assets/img/gallery/"; $target_file = $target_dir . $file_name; // Move uploaded file to target directory if (move_uploaded_file($_FILES['multiple_images']['tmp_name'][$key], $target_file)) { // Insert into database $sql = "INSERT INTO gallery_images (cat_id, image) VALUES ('$cat_id', '$file_name')"; mysqli_query($con, $sql); } } $alert = 'Gallery images added successfully.'; $alert_class = 'alert-success'; } else { $alert = 'Error: No images were uploaded.'; $alert_class = 'alert-danger'; } } // Update Gallery Image if (isset($_POST['action']) && $_POST['action'] === 'update') { $cat_id = mysqli_real_escape_string($con, $_POST['cat_id']); // Handle new image uploads (optional) if (!empty($_FILES['new_images']['name'][0])) { $allowed_types = ['image/jpeg', 'image/png', 'image/gif']; $max_size = 4 * 1024 * 1024; // 4MB foreach ($_FILES['new_images']['tmp_name'] as $key => $tmp_name) { $file_type = $_FILES['new_images']['type'][$key]; $file_size = $_FILES['new_images']['size'][$key]; $file_name = basename($_FILES['new_images']['name'][$key]); // Validate file type and size if (!in_array($file_type, $allowed_types)) { $alert = 'Error: Invalid file type. Only JPEG, PNG, and GIF are allowed.'; $alert_class = 'alert-danger'; return; } if ($file_size > $max_size) { $alert = 'Error: File size exceeds the maximum limit of 4MB.'; $alert_class = 'alert-danger'; return; } // Move uploaded file to target directory $target_dir = "assets/img/gallery/"; $target_file = $target_dir . $file_name; if (move_uploaded_file($_FILES['new_images']['tmp_name'][$key], $target_file)) { // Insert new image into the database $sql = "INSERT INTO gallery_images (cat_id, image) VALUES ('$cat_id', '$file_name')"; mysqli_query($con, $sql); } } } $alert = 'Gallery images updated successfully.'; $alert_class = 'alert-success'; } // Delete Gallery Image if (isset($_POST['action']) && $_POST['action'] === 'delete') { $id = mysqli_real_escape_string($con, $_POST['id']); // Fetch image filename before deletion $sql = "SELECT image FROM gallery_images WHERE id = $id"; $result = mysqli_query($con, $sql); $row = mysqli_fetch_assoc($result); if ($row) { $image_path = "assets/img/gallery/" . $row['image']; // Delete file from server if (file_exists($image_path)) { unlink($image_path); } // Delete record from database $sql = "DELETE FROM gallery_images WHERE id = $id"; if (mysqli_query($con, $sql)) { $alert = 'Gallery image deleted successfully.'; $alert_class = 'alert-success'; } else { $alert = 'Error: ' . mysqli_error($con); $alert_class = 'alert-danger'; } } else { $alert = 'Error: Image not found.'; $alert_class = 'alert-danger'; } } } // Fetch All Gallery Categories function fetchCategories($con) { $sql = "SELECT * FROM gallery_category"; $result = mysqli_query($con, $sql); $categories = []; while ($row = mysqli_fetch_assoc($result)) { $categories[] = $row; } return $categories; } // Fetch All Gallery Images Grouped by Category function fetchGalleryImagesGroupedByCategory($con) { $sql = "SELECT gc.id AS category_id, gc.title AS category_title, GROUP_CONCAT(gi.image) AS images FROM gallery_category gc LEFT JOIN gallery_images gi ON gc.id = gi.cat_id GROUP BY gc.id, gc.title"; $result = mysqli_query($con, $sql); $grouped_images = []; while ($row = mysqli_fetch_assoc($result)) { $grouped_images[] = $row; } return $grouped_images; } ?>