-
Notifications
You must be signed in to change notification settings - Fork 0
/
questions.php
executable file
·148 lines (142 loc) · 3.59 KB
/
questions.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
<?php
include_once "template.php";
session_start();
if (!isset($_SESSION['username']))
{
header("location:login.php");
}
function html($topics, $hidden=null, $text=null, $choices=null, $answer=null)
{
if ($choices == null)
{
$choices = "<input type='text' name='ch1'>
<input type='button' name='add_ch' value='Add Choices'>";
}
$html = "
<form method='POST' action='saveq.php'>
$hidden
<span>
Question:
</span>
<br>
<textarea name='text' rows='4' cols='40'>$text</textarea>
<br>
<span>
Topic / Sub-Topic:
</span>
<select name='topic' id='topic'>
<option value='0'>
Select a Topic
</option>
$topics
<option value='-1'>
Other
</option>
</select>
<br id='topicbr'>
<span>
Choices:
</span>
<br>
$choices
<br>
<span>
Answer:
</span>
<textarea name='answer' rows='4' cols='40'>$answer</textarea>
<input type='submit' name='submit' value='Submit'>
</form>
<script>
window.onload = function()
{
topic = document.getElementById('topic');
if(topic.value==-1)
{
thtml = document.createElement('input');
thtml.type = 'text';
thtml.name = 'topic';
topicbr = document.getElementById('topicbr');
document.querySelector('form').insertBefore(thtml, topicbr);
shtml = document.createElement('input');
shtml.type = 'text';
shtml.name = 'stopic';
document.querySelector('form').insertBefore(shtml, topicbr);
}
topic.onchange = function()
{
console.log(topic.value);
if(topic.value==-1)
{
html = document.createElement('input');
html.type = 'text';
html.name = 'topic';
topicbr = document.getElementById('topicbr');
document.querySelector('form').insertBefore(html, topicbr);
}
else
{
document.querySelector('input[type=text][name=topic]').remove();
}
}
}
</script>";
template($html);
}
function topics($selected=null)
{
global $dbo;
$options = null;
$stmt = $dbo -> prepare("SELECT topic_id, topic, stopic FROM topics");
$stmt -> execute();
$topics = $stmt -> fetchAll();
foreach ($topics as $row)
{
$options .= "
<option value='{$row['topic_id']}'";
if ($row['topic_id']===$selected)
{
$options .= " selected";
}
$options .= ">
{$row['topic']} / {$row['stopic']}
</option>";
}
return $options;
}
if ($_SERVER['REQUEST_METHOD'] == "GET")
{
$topics = topics();
html($topics);
}
else if ($_SERVER['REQUEST_METHOD'] == "POST")
{
$qid = $_POST['selected'][0];
$stmt = $dbo -> prepare("SELECT `text`, topic_id, answer, choice FROM questions, choices, topics WHERE qid=:qid AND qid=question_id AND toid = topic_id");
$stmt -> bindParam(':qid', $qid);
$stmt -> execute();
$rows = $stmt -> fetchAll(PDO::FETCH_ASSOC);
$ctexts = array();
foreach($rows as $row)
{
foreach ($row as $key => $value)
{
${$key} = $value;
if ($choice != ""&&!in_array($choice, $ctexts))
{
$ctexts[] = $choice;
}
}
}
$topics = topics($topic_id);
$hidden = "<input type='hidden' name='qid' value='$qid'>";
$choices = null;
foreach ($ctexts as $value) {
$choices .= "<input type='text' name='ch1' value='$value'><br>";
}
html($topics, $hidden, $text, $choices, $answer);
}
else
{
throw new Exception("Unknown request method!");
}
?>