1<?php
2require_once __DIR__ . '/../includes/bootstrap.php';
3
4$q = get('q');
5if (mb_strlen($q) < 2) json_out(['topics' => [], 'posts' => []]);
6
7$like = '%' . $q . '%';
8
9
10$topics = DB::rows(
11 "SELECT t.id, t.title, t.slug, c.name AS cat, c.color AS cat_color
12 FROM topics t
13 JOIN categories c ON c.id = t.category_id
14 WHERE t.title LIKE ?
15 ORDER BY t.last_post_at DESC
16 LIMIT 6",
17 [$like]
18);
19
20
21$posts = DB::rows(
22 "SELECT p.id AS post_id, p.content, p.post_num,
23 t.title AS topic_title, t.slug AS topic_slug,
24 u.username
25 FROM posts p
26 JOIN topics t ON t.id = p.topic_id
27 JOIN users u ON u.id = p.user_id
28 WHERE p.content LIKE ? AND p.deleted = 0
29 ORDER BY p.created_at DESC
30 LIMIT 5",
31 [$like]
32);
33
34json_out(['topics' => $topics, 'posts' => $posts]);