-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_questions_trivia_game.php
86 lines (69 loc) · 2.36 KB
/
read_questions_trivia_game.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
<?php
require_once 'assets/config/config.php';
require_once "vendor/autoload.php";
use PhotoTech\ErrorHandler;
use PhotoTech\Database;
use PhotoTech\Trivia;
$errorHandler = new ErrorHandler();
// Register the exception handler method
set_exception_handler([$errorHandler, 'handleException']);
$database = new Database();
$pdo = $database->createPDO();
$trivia = new Trivia($pdo);
/*
* Get Category from the FETCH statement from javascript
*/
$category = htmlspecialchars($_GET['category']);
if (isset($category)) { // Get rid of $api_key if not using:
$data = $trivia->fetch_data($category); // Fetch the data from the Database Table:
$finalOutput = []; // Final Output:
$ansColumns = []; // 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 $question_data) {
if ($question_data['hidden'] === 'yes') {
continue;
}
foreach ($question_data as $key => $value) {
/*
* Use Switch Function to convert to the right array format
* that is based on the key (index) of that array.
*/
switch ($key) {
case 'answer1':
$ansColumns['answers'][$index] = $value;
break;
case 'answer2':
$ansColumns['answers'][$index + 1] = $value;
break;
case 'answer3':
$ansColumns['answers'][$index + 2] = $value;
break;
case 'answer4':
$ansColumns['answers'][$index + 3] = $value;
break;
}
} // End of Inner foreach
$finished = array_merge($question_data, $ansColumns);
$finalOutput[$indexArray] = $finished;
$indexArray++;
} // End of Outer foreach
output($finalOutput); // Send properly formatted array back to javascript:
}
/*
* After converting data array to JSON send back to javascript using
* this function.
*/
function output($output): void
{
http_response_code(200);
try {
echo json_encode($output, JSON_THROW_ON_ERROR);
} catch (JsonException) {
}
}