[ 'p1' => [ 'id' => 'p1', 'name' => 'Brushless Cordless Impact Drill Kit 18V', 'description' => 'A high-performance 18V Lithium-ion impact drill with charger and two batteries. Features a brushless motor for extended life and power.', 'price' => 99.00, 'originalPrice' => 129.00, 'imageUrl' => 'https://placehold.co/400x300/F3F4F6/9CA3AF?text=DRILL', 'iconClass' => 'fa-solid fa-drill', 'rating' => 4.8, 'reviews' => 120, 'stock' => 14, 'badge' => 'IN STOCK', 'badgeColor' => 'bg-green-500', 'createdAt' => time() - 3600 ], 'p2' => [ 'id' => 'p2', 'name' => 'Heavy Duty Tape Measure 25ft Magnetic', 'description' => 'Professional grade 25-foot tape measure with magnetic tip, designed for durability and easy reading on construction sites.', 'price' => 19.99, 'originalPrice' => 23.50, 'imageUrl' => 'https://placehold.co/400x300/F3F4F6/9CA3AF?text=TAPE', 'iconClass' => 'fa-solid fa-tape', 'rating' => 4.9, 'reviews' => 850, 'stock' => 55, 'badge' => '-15%', 'badgeColor' => 'bg-red-500', 'createdAt' => time() - 1800 ] ], 'banners' => [ 'b1' => [ 'id' => 'b1', 'title' => 'Spring Sale: 20% Off All Power Tools!', 'subtitle' => 'Limited time offer. Stock up on essential gear now.', 'color' => '#DC2626', // Red 600 'order' => 1, 'createdAt' => time() - 5000 ] ], 'cart' => [] // User's private cart ]; } } init_db(); // --- 2. PHP Request Handling (CRUD & API Endpoint) --- // Handle AJAX data retrieval if (isset($_GET['action']) && $_GET['action'] == 'get_data') { header('Content-Type: application/json'); // Sort products by name and banners by order before sending $products = $_SESSION['db']['products']; usort($products, fn($a, $b) => $a['name'] <=> $b['name']); $banners = $_SESSION['db']['banners']; usort($banners, fn($a, $b) => $a['order'] <=> $b['order']); echo json_encode([ 'products' => array_values($products), 'banners' => array_values($banners), 'cart' => $_SESSION['db']['cart'] ], JSON_UNESCAPED_UNICODE); exit; } // Handle POST requests for CRUD operations if (isset($_POST['action'])) { header('Content-Type: application/json'); $response = ['success' => false, 'message' => 'Invalid action.']; $data = $_SESSION['db']; try { switch ($_POST['action']) { // --- Product CRUD --- case 'add_product': $id = uniqid('p'); // Note: Admin form input is simpler than mock data above, using defaults $new_product = [ 'id' => $id, 'name' => trim($_POST['name']), 'description' => trim($_POST['description']), 'price' => floatval($_POST['price']), 'originalPrice' => floatval($_POST['price']), // Default 'imageUrl' => trim($_POST['imageUrl']), 'iconClass' => 'fa-solid fa-wrench', // Default icon for admin adds 'rating' => 5.0, 'reviews' => 0, 'stock' => 10, 'badge' => 'NEW', 'badgeColor' => 'bg-green-500', 'createdAt' => time() ]; if (!$new_product['name'] || $new_product['price'] <= 0) { throw new Exception("Validation failed: Invalid name or price."); } $data['products'][$id] = $new_product; $response = ['success' => true, 'message' => 'Product added successfully.']; break; case 'delete_product': $id = $_POST['id']; if (isset($data['products'][$id])) { unset($data['products'][$id]); $response = ['success' => true, 'message' => 'Product deleted successfully.']; } else { throw new Exception("Product not found."); } break; // --- Banner CRUD --- case 'add_banner': $id = uniqid('b'); $new_banner = [ 'id' => $id, 'title' => trim($_POST['title']), 'subtitle' => trim($_POST['subtitle']), 'color' => trim($_POST['color']), 'order' => intval($_POST['order']), 'createdAt' => time() ]; if (!$new_banner['title'] || !$new_banner['subtitle']) { throw new Exception("Validation failed: Title and Subtitle are required."); } $data['banners'][$id] = $new_banner; $response = ['success' => true, 'message' => 'Banner added successfully.']; break; case 'delete_banner': $id = $_POST['id']; if (isset($data['banners'][$id])) { unset($data['banners'][$id]); $response = ['success' => true, 'message' => 'Banner deleted successfully.']; } else { throw new Exception("Banner not found."); } break; // --- Cart Management --- case 'update_cart': $updated_items = json_decode($_POST['items'], true); $data['cart'] = $updated_items ?: []; $response = ['success' => true, 'message' => 'Cart updated successfully.']; break; default: throw new Exception("Action not recognized."); } // Save changes to the session $_SESSION['db'] = $data; } catch (Exception $e) { $response = ['success' => false, 'message' => $e->getMessage()]; } echo json_encode($response, JSON_UNESCAPED_UNICODE); exit; } ?> ProBuild Hardware
PB

ProBuild

Categories

See All
Tools
Paint
Electrical
Plumbing
Safety

Trending Hardware

Loading products...

Shop by Project

Bathroom Renovation

Faucets, caulk, tiling tools & more.

Garden & Patio

Lumber, stains, outdoor power tools.

Shopping Cart

Total: $0.00