/
question-detail.php
155 lines (118 loc) · 4.48 KB
/
question-detail.php
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Question Detail</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<link href='https://fonts.googleapis.com/css?family=Roboto:400,100,300,300italic' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<header id="fixed-header">
<a href="./index.php"> <img id="logo" src="logo.png" alt="Quora"/> </a>
<?php
session_start();
$loggedin = isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true;
if($loggedin) : ?>
<a href="./logout.php" id="header-login">Logout</a>
<?php else : ?>
<a href="./form-login.php" id="header-login">Login</a>
<?php endif; ?>
</header>
<?php if($loggedin) : ?>
<a href="./form-question.php"><img src="FAB.png" alt="Submit a question" title="Submit a question" id="FAB" /></a>
<?php endif; ?>
<div id="user-question">
<?php
require_once('../../mysqli-connect.php');
$qid = $_GET["qid"];
$sql = "SELECT * FROM Questions WHERE id=" . $qid;
$response = $conn->query($sql);
if($response) {
if(mysqli_num_rows($response) > 0) {
while($row = mysqli_fetch_array($response)){
$avatarpath = "./avatars/avatar-" . $row["user_id"] . ".png";
if(!file_exists($avatarpath)) {
$avatarpath = "avatar.png";
}
$sql2 = "SELECT * FROM Users WHERE id=" . $row["user_id"];
$response2 = $conn->query($sql2);
$user = mysqli_fetch_array($response2);
?>
<img src=<?php echo $avatarpath; ?> alt="Avatar" id="qd-avatar" />
<p id="qd-username"><?php echo $user["username"]; ?></p>
<br />
<p id="qd-question"><?php echo $row["question_text"]; ?></p>
<?php if($loggedin) { ?>
<br /> <hr />
<a href=<?php echo "./form-answer.php?qid=" . $row["id"]; ?> class="answer-button">Answer!</a>
<?php } //end if
} //end while
?>
</div>
<div id="answer-list">
<?php
$sql = "SELECT * FROM Answers WHERE question_id=" . $qid . " ORDER BY upvotes-downvotes DESC";
$response = $conn->query($sql);
if($response) {
if(mysqli_num_rows($response) > 0) {
while($row = mysqli_fetch_array($response)) {
$user_id = $row["user_id"];
$sql = "SELECT * FROM Users WHERE id=" . $user_id;
$result = $conn->query($sql);
if($user = mysqli_fetch_array($result)) {}
?>
<div class="qd-answer">
<?php
if($loggedin){
$sql = "SELECT value FROM Votes WHERE user_id=" . $_SESSION["id"] . " AND answer_id=" . $row["id"];
$result = $conn->query($sql);
}
?>
<aside class="votes">
<img src="upvote-grey.png" alt=<?php echo "\"" . $row["id"] . "\""; ?> class="upvote-arrow
<?php
if($result) {
$vote = mysqli_fetch_array($result);
if($vote["value"] == 1) { echo "upvote-highlight"; }
else { echo "upvote-unhighlight"; }
} else { echo "upvote-unhighlight"; }
?>" />
<label class="upvote-count"><?php echo $row["upvotes"]; ?> Upvotes</label>
<br />
<img src="downvote-grey.png" alt=<?php echo "\"" . $row["id"] . "\""; ?> class="downvote-arrow
<?php
if($result) {
if($vote["value"] == -1) { echo "downvote-highlight"; }
else { echo "downvote-unhighlight"; }
} else { echo "downvote-unhighlight"; }
?>" />
<label class="downvote-count"><?php echo $row["downvotes"]; ?> Downvotes</label>
</aside>
<aside class="answerer-info">
<label class="answer-username"><?php echo $user["username"] ?></label>
</aside>
<hr />
<label class="answer-text"><?php echo $row["answer_text"]; ?></label>
</div>
<?php } //end while loop
} //end if(rows > 0)
else {
?>
<div class="qd-answer post-display no-answers">There are no answers yet.<br />Be the first!</div>
<?php
} //end else(no answers)
mysqli_free_result($response);
} //end if(response)
} //end if(question id exists)
} //end if (response)
else {
echo "Error.</div>";
}
?>
</div>
<?php if($loggedin) { ?>
<script type="text/javascript" src="script-qd.js"></script>
<?php } ?>
</body>
</html>