diff --git a/public/page/community.php b/public/page/community.php index 0415666..950df46 100644 --- a/public/page/community.php +++ b/public/page/community.php @@ -35,6 +35,7 @@ $threads = []; $configCommunity = require __DIR__ . '/../../config/community.php'; $pointsCfg = $configCommunity['points'] ?? []; $levelsCfg = $configCommunity['levels'] ?? []; +$search = trim((string)($_GET['q'] ?? '')); if ($pdo) { $pdo->prepare('CREATE TABLE IF NOT EXISTS forum_posts ( id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, @@ -46,7 +47,22 @@ if ($pdo) { INDEX idx_thread (thread_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci')->execute(); - $sql = 'SELECT ft.id, ft.title, ft.body, ft.created_at, + $conditions = []; + $params = []; + if ($search !== '') { + $tokens = preg_split('/\\s+/', $search); + $i = 0; + foreach ($tokens as $tok) { + $tok = trim($tok); + if ($tok === '') continue; + $ph = ':t' . $i++; + $conditions[] = "(ft.title LIKE $ph OR ft.body LIKE $ph)"; + $params[$ph] = '%' . $tok . '%'; + } + } + $whereSearch = $conditions ? ('AND ' . implode(' AND ', $conditions)) : ''; + + $sql = "SELECT ft.id, ft.title, ft.body, ft.created_at, u.id as uid, u.created_at as user_created, p.display_name, (SELECT COUNT(*) FROM forum_posts fp WHERE fp.thread_id = ft.id) AS answers, @@ -55,9 +71,12 @@ if ($pdo) { FROM forum_threads ft JOIN users u ON u.id = ft.user_id LEFT JOIN user_profiles p ON p.user_id = u.id + WHERE 1=1 $whereSearch ORDER BY ft.created_at DESC - LIMIT 50'; - $threads = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC) ?: []; + LIMIT 50"; + $stmt = $pdo->prepare($sql); + $stmt->execute($params); + $threads = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: []; } function compute_points(array $row, \PDO $pdo, array $pointsCfg): float { @@ -96,40 +115,38 @@ function membership_level(float $points, array $levels): array {
- -