-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts.php
More file actions
55 lines (41 loc) · 1.34 KB
/
posts.php
File metadata and controls
55 lines (41 loc) · 1.34 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
<?php
$conn = mysqli_connect('localhost', 'root', '', 'maksitwitter');
$query = "SELECT posts.id, posts.content, posts.author, posts.date_created, posts.likes,
users.username, users.pfp_path
FROM posts
JOIN users ON posts.author = users.id
ORDER BY $sort DESC";
$result = mysqli_query($conn, $query);
if (!$result) {
exit;
}
while ($row = mysqli_fetch_assoc($result)) {
$author_name = $row['username'] ?? 'Unknown';
$pfp = $row['pfp_path'] ?? 'default_pfp.png';
$date = date('M j, Y · H:i', strtotime($row['date_created']));
?>
<div class="post">
<p><?php echo nl2br(htmlspecialchars($row['content'])) ?></p><br>
<p>Posted by</p>
<img src="<?php echo htmlspecialchars($pfp); ?>" width="40" height="40">
<p><?php echo htmlspecialchars($author_name) ?> on <?php echo $date ?></p>
<p>Likes: <span class="likes-count"><?php echo $row['likes']; ?></span></p>
<button
class="like-btn"
data-postid="<?php echo $row['id']; ?>">
<?php
$postid = $row['id'];
$userid = $_SESSION['id'];
$liked = mysqli_query($conn, "SELECT * FROM post_likes WHERE post_id='$postid' AND user_id='$userid'");
if (!$liked || mysqli_num_rows($liked) == 0) {
echo "Like";
} else {
echo "Liked";
}
?>
</button>
</div>
<?php
}
mysqli_close($conn);
?>