-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsedata.php
executable file
·65 lines (58 loc) · 2.48 KB
/
parsedata.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
<?php
/* php function for parsing data into a correctly formatted json object */
/* 1. create database connection */
include('database_config.php');
//$data_array = json_decode(file_get_contents('php://input'), true);
try {
/* using '->' to make non-static function calls for objects */
$conn = new PDO("mysql:host=$servername;port=$port;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/* 2. perform database query:
* query() executes an SQL statement, returning a result set as a PDOStatement object */
$stmt = $conn->query("SELECT `subject_id`, `responses`, `character`, `instructor`, `term`, `year` FROM `$table_data`");
/* initialize new array */
$all_data = array();
/* fetches the next row from the table set */
while($row = $stmt->fetch()) { // while there are still rows to be fetched
if (isset($row['responses'])) {
/* $reponses saves an associative array instead of an object because
* true is passed as the second argument */
$responses = json_decode($row['responses'], true);
/* create array that contains all necessary keys and values */
$characters = array('subject_id' => $row['subject_id'],
'instructor' => $row['instructor'],
'term' => $row['term'] . " " . $row['year'],
'character' => $row['character'],
'expression' => intval($responses["Q0"]),
'awareness' => intval($responses["Q1"]),
'hope' => intval($responses["Q2"]),
'embarrassment' => intval($responses["Q3"]),
'empathy' => intval($responses["Q4"]),
'fear' => intval($responses["Q5"]),
'hunger' => intval($responses["Q6"]),
'joy' => intval($responses["Q7"]),
'memory' => intval($responses["Q8"]),
'morality' => intval($responses["Q9"]),
'pain' => intval($responses["Q10"]),
'personality' => intval($responses["Q11"]),
'attainment' => intval($responses["Q12"]),
'pleasure' => intval($responses["Q13"]),
'pride' => intval($responses["Q14"]),
'anger' => intval($responses["Q15"]),
'self-restraint' => intval($responses["Q16"]),
'thought' => intval($responses["Q17"])
);
/* push each $characters array into the $all_data array */
array_push($all_data, $characters);
}
}
/* return json object */
$json = json_encode($all_data);
echo $json;
} catch(PDOException $e) {
$r = array('success' => false, 'error_message' => $e->getMessage());
echo json_encode($r);
}
/* 3. close database connection */
$conn = null;
?>