« Back to History
category_manage.php
|
20260920_164915.php
Initial Domain Snapshot
Copy Code
<?php declare(strict_types=1); require_once __DIR__ . '/auth.php'; require_login(); $SHOP_ID = (int)$_SESSION['owner']['shop_id']; $error = ''; $success = ''; /* ---------- UPDATE / TOGGLE ---------- */ if (isset($_POST['action'])) { $cat_id = (int)($_POST['category_id'] ?? 0); if ($_POST['action'] === 'toggle') { $pdo->prepare(" UPDATE category SET is_active = IF(is_active=1,0,1) WHERE category_id=? AND shop_id=? ")->execute([$cat_id, $SHOP_ID]); $success = 'Category status updated.'; } if ($_POST['action'] === 'edit') { $name = trim($_POST['category_name'] ?? ''); if ($name === '') { $error = 'Category name required.'; } else { try { $pdo->prepare(" UPDATE category SET category_name=? WHERE category_id=? AND shop_id=? ")->execute([$name, $cat_id, $SHOP_ID]); $success = 'Category updated.'; } catch (Throwable $e) { $error = 'Duplicate category name.'; } } } } /* ---------- ADD CATEGORY ---------- */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && !isset($_POST['action'])) { $name = trim($_POST['category_name'] ?? ''); $has_subtype = isset($_POST['has_subtype']) ? 1 : 0; if ($name === '') { $error = 'Category name required.'; } else { try { $pdo->prepare(" INSERT INTO category (shop_id, category_name, has_subtype, is_active) VALUES (?, ?, ?, 1) ")->execute([$SHOP_ID, $name, $has_subtype]); $success = 'Category added.'; } catch (Throwable $e) { $error = 'Category already exists.'; } } } /* ---------- FETCH ---------- */ $rows = $pdo->prepare(" SELECT category_id, category_name, has_subtype, is_active FROM category WHERE shop_id=? ORDER BY category_name "); $rows->execute([$SHOP_ID]); $data = $rows->fetchAll(); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Category Manage</title> <meta name="viewport" content="width=device-width,initial-scale=1"> <link rel="stylesheet" href="assets/css/main.css"> </head> <body class="font-md"> <div class="header"><h2>Category Manage</h2></div> <div class="container"> <div class="card"> <?php if($error): ?><div class="alert alert-danger"><?=h($error)?></div><?php endif; ?> <?php if($success): ?><div class="alert alert-success"><?=h($success)?></div><?php endif; ?> <form method="post"> <div class="form-group"> <label>Category Name</label> <input type="text" name="category_name" required> </div> <div class="form-group"> <label><input type="checkbox" name="has_subtype"> Has Subtype (Camera)</label> </div> <button class="btn btn-primary">Add Category</button> </form> </div> <div class="card"> <h3>Categories</h3> <?php foreach($data as $c): ?> <form method="post" class="mb-sm"> <input type="hidden" name="category_id" value="<?=$c['category_id']?>"> <input type="text" name="category_name" value="<?=h($c['category_name'])?>" class="mb-sm"> <button name="action" value="edit" class="btn btn-secondary">Save</button> <button name="action" value="toggle" class="btn btn-danger"> <?=$c['is_active'] ? 'Disable' : 'Enable'?> </button> <?php if($c['has_subtype']): ?> <div class="text-muted">Subtype locked (Camera)</div> <?php endif; ?> </form> <hr> <?php endforeach; ?> </div> </div> </body> </html>