prepare("SELECT id, title, content, excerpt, image, author, created_at FROM news WHERE status = 'active' ORDER BY created_at DESC"); $stmt->execute(); $newsArticles = $stmt->fetchAll(PDO::FETCH_ASSOC); // Sanitize content for safe display (no foreach by reference) foreach ($newsArticles as $key => $article) { // Title $newsArticles[$key]['title'] = htmlspecialchars($article['title']); // Excerpt: use provided excerpt if available, otherwise build from content if (!empty($article['excerpt'])) { $newsArticles[$key]['excerpt'] = htmlspecialchars($article['excerpt']); } else { $cleanContent = strip_tags($article['content'] ?? ''); // safe multibyte substring $newsArticles[$key]['excerpt'] = htmlspecialchars(mb_substr($cleanContent, 0, 150)) . '...'; } // Author default in Hindi $newsArticles[$key]['author'] = htmlspecialchars($article['author'] ?? 'टीम किसानएक्स'); // Normalize image name (no change to design — just ensure it's safe to echo) $newsArticles[$key]['image'] = htmlspecialchars($article['image'] ?? ''); } // Optional: remove exact-duplicate titles if your DB has duplicate rows $seenTitles = []; $uniqueNews = []; foreach ($newsArticles as $article) { $titleKey = trim(mb_strtolower($article['title'])); if (isset($seenTitles[$titleKey])) { // skip duplicate title continue; } $seenTitles[$titleKey] = true; $uniqueNews[] = $article; } // Use $uniqueNews for rendering to avoid showing the same titled item twice $newsArticles = $uniqueNews; include 'header.php'; include 'navbar.php'; ?>