-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.php
More file actions
161 lines (145 loc) · 5.53 KB
/
thread.php
File metadata and controls
161 lines (145 loc) · 5.53 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
session_start();
$id = $_GET['threadid'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<?php include './includes/_links.php' ?>
</head>
<body>
<?php include './includes/_header.php' ?>
<div class="container" style="min-height: 80vh;margin-top:20vh">
<?php
$plo = pdo_connect_mysql();
$stmt = $plo->prepare("SELECT * FROM forum_threads WHERE thread_id=?");
$stmt->execute([$id]);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$cat = $result[0]['thread_title'];
$desc = $result[0]['thread_desc'];
?>
<!-- FORUM RULES AND CATEGORY DETAILS -->
<div class="container my-4">
<div class="jumbotron m-3">
<h4 class="display-4"><?=$cat?></h1>
<p class="lead"><?=$desc?></p>
<hr class="my-4">
<p>No Spam / Advertising / Self-promote in the forums. <br>
Do not post copyright-infringing material. <br>
Do not post “offensive” posts, links or images. <br>
Do not cross post questions. <br>
Do not PM users asking for help. <br>
Remain respectful of other members at all </p>
</div>
</div>
<?php
//show comment form if user is logged in
if(isset($_SESSION['loggedin']) and $_SESSION['loggedin'] ==true): ?>
<!-- form to submit answers -->
<div class="container my-4">
<div class="jumbotron m-3">
<form action="postA.php" method="POST">
<h4 class="py-2">Post a comment</h4>
<input type="hidden" name="thread_id" value="<?php echo $id ?>">
<div class="form-group">
<label for="desc">Type your answer</label>
<textarea class="form-control" id="comment" name="answer" rows="5" required></textarea>
</div>
<button type="submit" class="btn btn-primary">Post comment</button>
</form>
</div>
</div>
<?php else : ?>
<!-- //tell user to login to comment if not logged in -->
<div class="container"><p class="lead">Log in to post a comment</p></div>
<?php endif ?>
<div class="container my-2">
<h3 class="py-2">Discussions</h3>
<!-- FETCH ANSWERS -->
<?php
/**
*FETCH USER'S LIKES IN CURRENT THREAD TO DISPLAY LIKE/UNLIKE BUTTON DYNAMICALLY
*/
if(isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])):
//array of id of all posts liked by user in current thread
$userLiked = fetchUserThreadLikes($id, $_SESSION['user_id']);
extract($userLiked);
endif;
$answers = fetchThread($id);
if(!$answers): ?>
<div class="jumbotron">
<div class="lead display-4">
This thread has no answers yet.
</div>
Be the first person to answer the question!
</div>
<?php
else:
foreach($answers as $answer):
?>
<div class="d-flex my-3">
<div class="flex-shrink-0">
<img src="./images/default user.png" style="max-width: 100px; max-height:100px" alt="...">
</div>
<div class="just">
<div class="flex-1 ms-3 mb-2 text-muted">
<?=$answer['answer_writer'] .' at '.$answer['answer_created']?>
</div>
<div class="px-3"> <?php echo $answer['answer_body']?></div>
<div class="px-3 likes" style="display: inline-block;">
<!-- LIKE AND UNLIKE ONLY IF USER IS LOGGED IN -->
<?php if(isset($_SESSION['user_id'])) : ?>
<!-- SHOW UNLIKE IF ALREADY LIKED AND VICE VERSA -->
<div class="like-btn-container text-primary">
<?php
if(!in_array($answer['answer_id'], $userLiked)):?>
<button class="text-primary" id="like<?php echo $answer['answer_id']?>" onclick="like(<?php echo $answer['answer_id'].','. $id?>,this.id)">Like</button>
<?php else: ?>
<button class="text-danger" id="like<?php echo $answer['answer_id']?>" onclick="unlike(<?php echo $answer['answer_id'].','. $id?>,this.id)">Unlike</button>
<?php endif; ?>
</div>
<?php
endif ;
?>
<!-- DISPLAY LIKE COUNT -->
<span id="likecount<?php echo $answer['answer_id']?>" name="likecount" ><?php echo $answer['answer_likes'] ?></span> likes
</div>
</div>
</div>
<?php endforeach;
endif;
?>
</div>
</div>
<style>
.jumbotron {
padding: 2rem 1rem;
margin-bottom: 2rem;
background-color: #e9ecef;
border-radius: .3rem;
}
.just{
display: flex;
flex-direction: column;
}
.question {
font-size: 1.4rem;
font-weight: bold;
}
.thread-link {
text-decoration: none;
color: black;
}
.like-btn-container button{
background: transparent;
box-shadow: 0px 0px 0px transparent;
border: 0px solid transparent;
text-shadow: 0px 0px 0px transparent;
}
</style>
<script src="js/likehandler.js"></script>
<?php include './includes/_footer.php' ?>