/
qdatabase.php
117 lines (93 loc) · 3.3 KB
/
qdatabase.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
<?php
require_once 'assets/config/config.php';
/*
* Database Connection
*/
$db_options = array(
/* important! use actual prepared statements (default: emulate prepared statements) */
PDO::ATTR_EMULATE_PREPARES => false
/* throw exceptions on errors (default: stay silent) */
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
/* fetch associative arrays (default: mixed arrays) */
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO('mysql:host=' . DATABASE_HOST . ';dbname=' . DATABASE_NAME . ';charset=utf8', DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);
/*
* Read Questions & Answers in from the Database Table Named 'trivia_questions'
*/
function readData($category, $pdo) {
$query = "SELECT id, question, answer1, answer2, answer3, answer4, category FROM trivia_questions WHERE category=:category";
$stmt = $pdo->prepare($query);
$stmt->execute([':category' => $category]);
$result = $stmt->fetchALL(PDO::FETCH_ASSOC);
return $result;
}
/* Makes it so we don't have to decode the json coming from JQuery */
header('Content-type: application/json');
/*
* Get Category from the FETCH statment from javascript
*/
$category = htmlspecialchars($_GET['category']);
$api_key = htmlspecialchars($_GET['api_key']); // Not Need if you are not using:
if (isset($api_key) && $api_key === $_SESSION['api_key'] && isset($category)) { // Get rid of $api_key if not using:
/*
* Call the readData Function
*/
$data = readData($category, $pdo);
$mData = []; // Temporary Array Placeholder:
$answers = []; // Answer Columns from Table Array:
$finished = []; // Finished Results:
$index = 0; // Index for answers array:
$indexArray = 0; // Index for database table array:
/*
* Put database table in proper array format in order that
* JSON will work properly.
*/
foreach ($data as $qdata) {
foreach ($qdata as $key => $value) {
switch ($key) {
case 'answer1':
$answers['answers'][$index] = $value;
break;
case 'answer2':
$answers['answers'][$index + 1] = $value;
break;
case 'answer3':
$answers['answers'][$index + 2] = $value;
break;
case 'answer4':
$answers['answers'][$index + 3] = $value;
break;
}
} // foreach inner
/*
* No Longer needed, but it wouldn't hurt if not unset
*/
unset($qdata['answer1']);
unset($qdata['answer2']);
unset($qdata['answer3']);
unset($qdata['answer4']);
$finished = array_merge($qdata, $answers);
$mData[$indexArray] = $finished;
$indexArray++;
}
output($mData); // Send properly formatted array back to javascript:
}
/*
* Throw error if something is wrong
*/
function errorOutput($output, $code = 500) {
http_response_code($code);
echo json_encode($output);
}
///*
// * If everything validates OK then send success message to Ajax / JavaScript
// */
/*
* After converting data array to JSON send back to javascript using
* this function.
*/
function output($output) {
http_response_code(200);
echo json_encode($output);
}