Discussions in
Start a New Discussion
Existing Discussions
No discussions found.
Started by:
prepare('SELECT * FROM communities WHERE id = ?'); $stmt->execute([$community_id]); $community = $stmt->fetch(); if (!$community) { header('Location: communities.php'); exit; } // Handle new discussion form submission $title = $content = ''; $errors = []; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $title = trim($_POST['title'] ?? ''); $content = trim($_POST['content'] ?? ''); if (empty($title)) { $errors[] = 'Title is required'; } if (empty($content)) { $errors[] = 'Content is required'; } if (empty($errors)) { try { $stmt = $pdo->prepare('INSERT INTO discussions (community_id, user_id, title, content) VALUES (?, ?, ?, ?)'); $stmt->execute([$community_id, $user_id, $title, $content]); header('Location: discussions.php?community_id=' . $community_id); exit; } catch (PDOException $e) { $errors[] = 'Database error: ' . $e->getMessage(); } } } // Search $search = isset($_GET['search']) ? trim($_GET['search']) : ''; // Pagination $limit = 10; $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $offset = ($page - 1) * $limit; // Build where clause for search $where_clause = 'WHERE d.community_id = ?'; $params = [$community_id]; if (!empty($search)) { $where_clause .= ' AND (d.title LIKE ? OR d.content LIKE ?)'; $params[] = '%' . $search . '%'; $params[] = '%' . $search . '%'; } // Fetch total number of discussions $stmt = $pdo->prepare('SELECT COUNT(*) FROM discussions d ' . $where_clause); $stmt->execute($params); $total_discussions = $stmt->fetchColumn(); $total_pages = ceil($total_discussions / $limit); // Fetch discussions for the current page $sql = 'SELECT d.*, u.name as user_name FROM discussions d JOIN users u ON d.user_id = u.id ' . $where_clause . ' ORDER BY d.created_at DESC LIMIT ? OFFSET ?'; $stmt = $pdo->prepare($sql); $params[] = $limit; $params[] = $offset; foreach ($params as $key => $value) { $stmt->bindValue($key + 1, $value, is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR); } $stmt->execute(); $discussions = $stmt->fetchAll(); ?>
No discussions found.
Started by: