0) { // Slug already exists, append a unique identifier $slug .= '-' . time(); } if ($is_edit) { // Update existing category $sql = "UPDATE product_categories SET name = '" . mysqli_real_escape_string($conn, $name) . "', slug = '" . mysqli_real_escape_string($conn, $slug) . "', parent_id = " . $parent_id . ", description = '" . mysqli_real_escape_string($conn, $description) . "', image = '" . mysqli_real_escape_string($conn, $image) . "', sort_order = " . $sort_order . ", updated_at = NOW() WHERE id = " . (int)$id; mysqli_query($conn, $sql); } else { // Insert new category $sql = "INSERT INTO product_categories (name, slug, parent_id, description, image, sort_order, status, created_at, updated_at) VALUES ( '" . mysqli_real_escape_string($conn, $name) . "', '" . mysqli_real_escape_string($conn, $slug) . "', " . $parent_id . ", '" . mysqli_real_escape_string($conn, $description) . "', '" . mysqli_real_escape_string($conn, $image) . "', " . $sort_order . ", 1, NOW(), NOW() )"; mysqli_query($conn, $sql); } // Redirect after save header("Location: ?keys=" . $keys); exit(); } // Handle bulk actions if ($act == 'postchk') { if (isset($_POST['chkbox']) && isset($_POST['chkact'])) { $chkact = $_POST['chkact']; $id_list = array(); foreach ($_POST['chkbox'] as $id) { if (is_numeric($id)) { $id_list[] = (int)$id; } } if (!empty($id_list)) { $ids = implode(',', $id_list); if ($chkact == '-1') { // Delete categories mysqli_query($conn, "DELETE FROM product_categories WHERE id IN($ids)"); // Reset parent_id for child categories of deleted categories mysqli_query($conn, "UPDATE product_categories SET parent_id = 0 WHERE parent_id IN($ids)"); } // Status update removed - all categories are enabled by default } } // Redirect after bulk action header("Location: ?Keys=" . $keys); exit(); } // Display edit form if ($act == 'edit' || $act == 'add') { $id = isset($_GET['id']) ? $_GET['id'] : ''; $is_edit = (!empty($id) && is_numeric($id) && $act == 'edit'); // Check for parent_id in URL for add mode if ($act == 'add' && isset($_GET['parent_id']) && is_numeric($_GET['parent_id'])) { $parent_id = intval($_GET['parent_id']); // Verify that the parent category exists $parent_check = mysqli_query($conn, "SELECT id FROM product_categories WHERE id = " . $parent_id); if (mysqli_num_rows($parent_check) == 0) { $parent_id = 0; // Reset if parent doesn't exist } } if ($is_edit) { $sql = "SELECT * FROM product_categories WHERE id = " . (int)$id; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { $row = mysqli_fetch_assoc($result); $name = htmlspecialcharsFix($row['name']); $slug = htmlspecialcharsFix($row['slug']); $description = htmlspecialcharsFix($row['description']); $image = htmlspecialcharsFix($row['image']); $parent_id = $row['parent_id']; $sort_order = $row['sort_order']; $status = $row['status']; } else { // Category not found header("Location: ?Keys=" . $keys); exit(); } } ?> 管理区域
分类名称
父分类
排序 数字越小越靠前
图片 分类图片URL
描述
管理区域
产品分类列表
"; echo "SQL: " . $sql; exit; } // Build category tree $categories = array(); $tree = array(); $search_matches = array(); // First pass: create an array of all categories while ($row = mysqli_fetch_assoc($result)) { $categories[$row['id']] = $row; $categories[$row['id']]['children'] = array(); // Check if this category matches the search criteria if (!empty($keyscode) && ( stripos($row['name'], $keyscode) !== false || stripos($row['description'], $keyscode) !== false)) { $search_matches[$row['id']] = true; } } // Second pass: build the tree structure foreach ($categories as $id => $category) { if ($category['parent_id'] == 0) { // Root category $tree[$id] = &$categories[$id]; } else { // Child category if (isset($categories[$category['parent_id']])) { $categories[$category['parent_id']]['children'][$id] = &$categories[$id]; } } } // If searching, mark parents of matching categories if (!empty($search_matches)) { foreach ($search_matches as $id => $match) { markParents($id, $categories); } } // Helper function to mark all parent categories function markParents($categoryId, &$categories) { if (!isset($categories[$categoryId])) { return; } $categories[$categoryId]['search_match'] = true; if ($categories[$categoryId]['parent_id'] > 0 && isset($categories[$categories[$categoryId]['parent_id']])) { markParents($categories[$categoryId]['parent_id'], $categories); } } // Function to display category tree recursively function displayCategoryTree($categories, $level = 0) { $temp_num = 0; foreach ($categories as $category) { // Skip if searching and neither this category nor any of its children match if (!empty($GLOBALS['keyscode']) && !isset($category['search_match']) && !categoryHasMatchingChild($category)) { continue; } $temp_num++; $indent = str_repeat('    ', $level); $prefix = $level > 0 ? $indent . '└─ ' : ''; $highlight = !empty($GLOBALS['keyscode']) && (stripos($category['name'], $GLOBALS['keyscode']) !== false || stripos($category['description'], $GLOBALS['keyscode']) !== false); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; // Display children recursively if (!empty($category['children'])) { displayCategoryTree($category['children'], $level + 1); } } return $temp_num; } // Helper function to check if a category has any child that matches search function categoryHasMatchingChild($category) { if (empty($category['children'])) { return false; } foreach ($category['children'] as $child) { if (isset($child['search_match']) || categoryHasMatchingChild($child)) { return true; } } return false; } if (count($tree) > 0) { displayCategoryTree($tree); } else { ?>
ID 分类名称 排序 类型 操作
' . $category['id'] . '' . $prefix; if ($highlight) { echo '' . htmlspecialcharsFix($category['name']) . ''; } else { echo htmlspecialcharsFix($category['name']); } // Show subcategory count if (!empty($category['children'])) { echo ' (' . count($category['children']) . '个子分类)'; } echo '' . $category['sort_order'] . ''; if ($level == 0) { echo '一级分类'; } else if ($level == 1) { echo '二级分类'; } else { echo $level + 1 . '级分类'; } echo ''; echo '修改'; echo ' | '; echo '添加子分类'; echo '
Sorry,没有找到"' . htmlspecialcharsFix($keyscode) . '"相关的分类,点击返回'; ?>