// news.php require_once 'includes/var.php'; $lang = getLang(); $isUrdu = $lang === 'ur'; // ---- Filters ---- $category = sanitize($_GET['category'] ?? '', $conn); $search = sanitize($_GET['search'] ?? '', $conn); $author = sanitize($_GET['author'] ?? '', $conn); $dateFrom = sanitize($_GET['date_from'] ?? '', $conn); $sortBy = in_array($_GET['sort'] ?? '', ['latest','oldest','popular']) ? $_GET['sort'] : 'latest'; $perPage = (int)($settings['news_per_page'] ?? 12); $page = max(1, (int)($_GET['page'] ?? 1)); $offset = ($page - 1) * $perPage; $tagSlug = sanitize($_GET['tag'] ?? '', $conn); // ---- Build query ---- $where = ["n.status='published'"]; if ($category) $where[] = "c.slug='$category'"; if ($search) $where[] = "(n.title_en LIKE '%$search%' OR n.title_ur LIKE '%$search%' OR n.excerpt_en LIKE '%$search%' OR n.excerpt_ur LIKE '%$search%')"; if ($author) $where[] = "u.name LIKE '%$author%'"; if ($dateFrom) $where[] = "DATE(n.published_at) >= '$dateFrom'"; if ($tagSlug) $where[] = "EXISTS(SELECT 1 FROM news_tags nt JOIN tags t ON nt.tag_id=t.id WHERE nt.news_id=n.id AND t.slug='$tagSlug')"; $whereStr = 'WHERE ' . implode(' AND ', $where); $orderBy = match($sortBy) { 'oldest' => 'n.published_at ASC', 'popular' => 'n.views DESC', default => 'n.published_at DESC' }; // Count $countRes = $conn->query("SELECT COUNT(*) as total FROM news n LEFT JOIN categories c ON n.category_id=c.id LEFT JOIN users u ON n.author_id=u.id $whereStr"); $total = $countRes->fetch_assoc()['total']; $totalPages = (int)ceil($total / $perPage); // Fetch $newsItems = []; $res = $conn->query("SELECT n.*, c.name_en as cat_en, c.name_ur as cat_ur, c.slug as cat_slug, u.name as author_name FROM news n LEFT JOIN categories c ON n.category_id=c.id LEFT JOIN users u ON n.author_id=u.id $whereStr ORDER BY $orderBy LIMIT $perPage OFFSET $offset"); while ($r = $res->fetch_assoc()) $newsItems[] = $r; // Authors for filter dropdown $authorsRes = $conn->query("SELECT DISTINCT u.id, u.name FROM users u JOIN news n ON n.author_id=u.id WHERE n.status='published' ORDER BY u.name"); $authorsList = []; while ($r = $authorsRes->fetch_assoc()) $authorsList[] = $r; // Active category name $activeCatName = ''; if ($category) { foreach ($navCategories as $c) { if ($c['slug'] === $category) { $activeCatName = $isUrdu ? $c['name_ur'] : $c['name_en']; break; } } } $pageTitle = $activeCatName ?: ($search ? ($isUrdu ? 'تلاش: '.$search : 'Search: '.$search) : ($isUrdu ? 'تمام خبریں' : 'All News')); require_once 'includes/head.php'; ?>