prepare($query); $stmt->bind_param("i", $id); } else { // Fetch all banners $query = "SELECT * FROM home_banner WHERE deleted_at IS NULL"; $stmt = $con->prepare($query); } $stmt->execute(); $result = $stmt->get_result(); $banners = []; while ($row = $result->fetch_assoc()) { $banner_id = $row['id']; // Fetch multiple images $image_query = "SELECT image_path FROM home_banner_images WHERE banner_id=?"; $img_stmt = $con->prepare($image_query); $img_stmt->bind_param("i", $banner_id); $img_stmt->execute(); $img_result = $img_stmt->get_result(); $images = []; while ($image_row = $img_result->fetch_assoc()) { $images[] = $image_row['image_path']; } $row['multiple_images'] = $images; $banners[] = $row; } echo json_encode(['success' => true, 'data' => $id ? $banners[0] : $banners]); break; case 'create': // Insert a new banner $title = $_POST['title'] ?? ''; $description = $_POST['description'] ?? ''; $single_image = ''; $upload_dir = '../home-banner/'; // Handle single image upload (optional) if (!empty($_FILES['single_image']['name'])) { if (!is_dir($upload_dir)) { mkdir($upload_dir, 0777, true); } $single_image = basename($_FILES['single_image']['name']); move_uploaded_file($_FILES['single_image']['tmp_name'], $upload_dir . $single_image); } // Insert banner into the database $query = "INSERT INTO home_banner (title, description, single_image) VALUES ('$title', '$description', '$single_image')"; $result = mysqli_query($con, $query); if ($result) { $banner_id = mysqli_insert_id($con); // Get the ID of the newly inserted banner // Handle multiple image uploads (optional) if (!empty($_FILES['multiple_images']['name'][0])) { foreach ($_FILES['multiple_images']['name'] as $key => $image_name) { $image_path = basename($image_name); move_uploaded_file($_FILES['multiple_images']['tmp_name'][$key], $upload_dir . $image_path); // Insert each image into the home_banner_images table $image_query = "INSERT INTO home_banner_images (banner_id, image_path) VALUES ('$banner_id', '$image_path')"; mysqli_query($con, $image_query); } } echo json_encode(['success' => true, 'message' => 'Banner added successfully']); } else { echo json_encode(['success' => false, 'error' => mysqli_error($con)]); } break; case 'update': $id = $_POST['id'] ?? 0; $title = $_POST['title'] ?? ''; $description = $_POST['description'] ?? ''; $upload_dir = '../home-banner/'; // Fetch the current number of images for the banner $image_query = "SELECT COUNT(*) AS image_count FROM home_banner_images WHERE banner_id=?"; $img_stmt = $con->prepare($image_query); $img_stmt->bind_param("i", $id); $img_stmt->execute(); $img_result = $img_stmt->get_result()->fetch_assoc(); $existing_image_count = $img_result['image_count']; // Count the number of new images being uploaded $new_images_count = !empty($_FILES['multiple_images']['name'][0]) ? count($_FILES['multiple_images']['name']) : 0; // Validate the total number of images if ($existing_image_count + $new_images_count > 10) { echo json_encode(['success' => false, 'error' => 'You can only upload a maximum of 10 images.']); exit; } // Prepare the update query $query = "UPDATE home_banner SET title=?, description=?, updated_at=NOW() WHERE id=?"; $stmt = $con->prepare($query); $stmt->bind_param("ssi", $title, $description, $id); // Handle single image update if (!empty($_FILES['single_image']['name'])) { if (!is_dir($upload_dir)) { mkdir($upload_dir, 0777, true); } $single_image = basename($_FILES['single_image']['name']); move_uploaded_file($_FILES['single_image']['tmp_name'], $upload_dir . $single_image); // Update the query to include the single image $query = "UPDATE home_banner SET title=?, description=?, single_image=?, updated_at=NOW() WHERE id=?"; $stmt = $con->prepare($query); $stmt->bind_param("sssi", $title, $description, $single_image, $id); } // Execute the update query if ($stmt->execute()) { // Handle multiple image updates if (!empty($_FILES['multiple_images']['name'][0])) { foreach ($_FILES['multiple_images']['name'] as $key => $image_name) { $image_path = basename($image_name); move_uploaded_file($_FILES['multiple_images']['tmp_name'][$key], $upload_dir . $image_path); // Insert each image into the home_banner_images table $image_query = "INSERT INTO home_banner_images (banner_id, image_path) VALUES (?, ?)"; $image_stmt = $con->prepare($image_query); $image_stmt->bind_param("is", $id, $image_path); $image_stmt->execute(); } } echo json_encode(['success' => true, 'message' => 'Banner updated successfully']); } else { echo json_encode(['success' => false, 'error' => $stmt->error]); } break; case 'delete_image': $banner_id = $_GET['banner_id'] ?? 0; $image = $_GET['image'] ?? ''; $upload_dir = '../home-banner/'; // Delete the image from the database $query = "DELETE FROM home_banner_images WHERE banner_id=? AND image_path=?"; $stmt = $con->prepare($query); $stmt->bind_param("is", $banner_id, $image); if ($stmt->execute()) { // Delete the image file from the server if (file_exists($upload_dir . $image)) { unlink($upload_dir . $image); } echo json_encode(['success' => true, 'message' => 'Image deleted successfully']); } else { echo json_encode(['success' => false, 'error' => $stmt->error]); } break; case 'delete': // Soft delete a banner $id = $_GET['id'] ?? 0; $query = "UPDATE home_banner SET deleted_at=NOW() WHERE id=$id"; $result = mysqli_query($con, $query); if ($result) { echo json_encode(['success' => true, 'message' => 'Banner deleted successfully']); } else { echo json_encode(['success' => false, 'error' => mysqli_error($con)]); } break; default: echo json_encode(['success' => false, 'error' => 'Invalid action']); } ?>