最新商品展示
/** * 构建安全查询并获取过滤后的商品数据 */ function fetch_filtered_goods(?string $keyword, ?string $season, ?string $type): array { global $pdo; $conditions = []; $params = []; // 关键字搜索条件(防SQL注入) if (!empty($keyword)) { $conditions[] = "(title LIKE ? OR goods_sn LIKE ?)"; $safe_keyword = safe_like_param($keyword); $params[] = $safe_keyword; $params[] = $safe_keyword; } // 季节过滤条件 if (!empty($season)) { $conditions[] = "season = ?"; $params[] = $season; } // 类型过滤条件 if (!empty($type)) { $conditions[] = "cate_type = ?"; $params[] = $type; } // 构建安全SQL $where_clause = empty($conditions) ? "" : "WHERE " . implode(" AND ", $conditions); $sql = "SELECT * FROM goods {$where_clause} ORDER BY id DESC LIMIT 20"; $stmt = $pdo->prepare($sql); $stmt->execute($params); return $stmt->fetchAll(PDO::FETCH_ASSOC) ?: []; } /** * 生成季节标签的颜色 */ function get_season_color(?string $season): string { $colors = [ '春' => '#f5576c', '夏' => '#4facfe', '秋' => '#fa709a', '冬' => '#a8edea' ]; return $colors[$season] ?? '#667eea'; } /** * 安全输出HTML内容 */ function safe_output(?string $value, string $default = ''): string { return htmlspecialchars($value ?? $default, ENT_QUOTES, 'UTF-8'); } /** * 格式化价格显示 */ function format_price($price): ?string { if (!is_numeric($price) || floatval($price) <= 0) { return null; } return '¥' . number_format(floatval($price), 2); } ?>
正在为您挑选...