-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_answers.php
executable file
·114 lines (90 loc) · 2.74 KB
/
sample_answers.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
<?php
require_once "header.php";
//$usernames = $_SESSION['username'];
$message = "";
//$usernames = $_SESSION['username'];
if (!isset($_SESSION['loggedInSkeleton'])) {
// user isn't logged in, display a message saying they must be:
echo "You must be logged in to view this page.<br>";
}
else
{
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
echo "Welcome ", $_SESSION['username'];
if ($_SESSION['username'] == "admin") {
$query = "SELECT * FROM sampleQuestions";
$result = mysqli_query($connection, $query);
$n = mysqli_num_rows($result);
// if we got some results then show them in a table:
if ($n > 0) {
echo <<<_END
<style>
table, th, td {border:1px solid black; align:center;}
th, td {
text-align:left;
padding: 10px;
}
tr:nth-child(even){background-color:#f96d6d}
tr:nth-child(odd){background-color:#e0f96d}
th{
background-color: #3f3f3e;
color:white;
}
</style>
_END;
echo "<table cellpadding='2' cellspacing='2'>";
echo "<tr><th>Question</th><th>Answer</th><th>Username</th></tr>";
// loop over all rows, adding them into the table:
for ($i = 0; $i < $n; $i++)
{
// fetch one row as an associative array (elements named after columns):
$row = mysqli_fetch_assoc($result);
$usernames = $row['username'];
// add it as a row in our table:
echo "<tr>";
echo "<td>{$row['sample_question']}</td><td>{$row['sample_answer']}</td><td>{$usernames}</td>";
echo "</tr>";
}
echo "</table>";
}
}
else {
$usernames = $_SESSION['username'];
$query = "SELECT * FROM sampleQuestions WHERE username = '$usernames' ";
$result = mysqli_query($connection, $query);
$n = mysqli_num_rows($result);
// if we got some results then show them in a table:
if ($n > 0) {
echo <<<_END
<style>
table, th, td {border:1px solid black; align:center;}
th, td {
text-align:left;
padding: 10px;
}
tr:nth-child(even){background-color:#f96d6d}
tr:nth-child(odd){background-color:#e0f96d}
th{
background-color: #3f3f3e;
color:white;
}
</style>
_END;
echo "<table cellpadding='2' cellspacing='2'>";
echo "<tr><th>Questions</th><th>Answers</th></tr>";
// loop over all rows, adding them into the table:
for ($i = 0; $i <$n; $i++)
{
$n= 5;
// fetch one row as an associative array (elements named after columns):
$row = mysqli_fetch_assoc($result);
// add it as a row in our table:
echo "<tr>";
echo "<td>{$row['sample_question']}</td><td>{$row['sample_answer']}</td>";
echo "</tr>";
}
echo "</table>";
}
}
}
?>