$name, 'slug' => $slug, 'description' => $description, 'is_active' => $isActive ]; if ($action === 'update' && $categoryId) { db()->update('categories', $data, 'category_id = :id', ['id' => $categoryId]); setFlash('success', 'Category updated'); } else { $data['category_id'] = generateId('cat_'); db()->insert('categories', $data); setFlash('success', 'Category created'); } } header('Location: /admin/categories.php'); exit; } if ($action === 'delete' && !empty($_POST['category_id'])) { db()->delete('categories', 'category_id = :id', ['id' => $_POST['category_id']]); setFlash('success', 'Category deleted'); header('Location: /admin/categories.php'); exit; } } // Get categories with product counts $categories = db()->fetchAll( "SELECT c.*, (SELECT COUNT(*) FROM products p WHERE p.category = c.slug OR p.category = c.name) as product_count FROM categories c ORDER BY c.name ASC" ); // Also get uncategorized products $uncategorizedCount = db()->count('products', "category IS NULL OR category = ''"); ?>
| Name | Slug | Products | Status | Actions |
|---|---|---|---|---|
| No categories yet. Create one above. | ||||
| = htmlspecialchars($cat['name']) ?> | = htmlspecialchars($cat['slug']) ?> | 0): ?> = $cat['product_count'] ?> products 0 products | Active Hidden | |
| Uncategorized | - | = $uncategorizedCount ?> products | Default | |