-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetchPosts.php
More file actions
executable file
·63 lines (57 loc) · 2.24 KB
/
fetchPosts.php
File metadata and controls
executable file
·63 lines (57 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'server/configure.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['section'])) {
// Get the user ID from the session or any other way you're identifying the user
$user_id = $_SESSION['user_id'];
// Connect to the database using PDO
try {
$pdo = new PDO("mysql:host=$DBHOST;dbname=$DBNAME;charset=utf8mb4", $DBUSER, $DBPASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Fetch posts based on the selected section
$section = $_POST['section'];
$sql = '';
switch ($section) {
case 'posts':
$sql = "SELECT * FROM posts WHERE creator = :user_id";
break;
case 'discussions':
// Fetch discussions based on user's comments on posts
$sql = "SELECT p.* FROM posts p INNER JOIN comments c ON p.id = c.postId WHERE c.userId = :user_id";
break;
case 'likes':
// Fetch liked posts based on user's interactions
$sql = "SELECT p.* FROM posts p INNER JOIN likes l ON p.id = l.postId WHERE l.userId = :user_id";
break;
case 'favorites':
// Fetch favorite posts based on user's interactions
$sql = "SELECT p.* FROM posts p INNER JOIN favorites f ON p.id = f.postId WHERE f.userId = :user_id";
break;
default:
// Invalid section
echo "Invalid section";
exit();
}
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->execute();
$posts = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Display fetched posts
foreach ($posts as $post) {
// Output each post as HTML
echo '<div class="post">';
echo '<h2>' . $post['title'] . '</h2>';
echo '<p>' . $post['content'] . '</p>';
echo '</div>';
}
} catch (PDOException $e) {
// Handle database errors
echo "Error: " . $e->getMessage();
}
} else {
// Invalid request
echo "Invalid request";
}
?>