forked from KamalChaya/WebDeck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_table_encode.php
More file actions
56 lines (48 loc) · 1.4 KB
/
php_table_encode.php
File metadata and controls
56 lines (48 loc) · 1.4 KB
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
<?php
function php_entity_encode($result)
{
$returnArray = array();
$tmp = array();
while($row = $result->fetch_object()) {
$tmp = $row;
array_push($returnArray, $tmp);
}
//Return the result for processing/echoing
return json_encode($returnArray);
}
function query_trim($query)
{
$ret_query = $query;
$ret_query = trim(preg_replace('/\t+/', ' ', $ret_query));
$ret_query = trim(preg_replace('/,/', '\,', $ret_query));
$ret_query = trim(preg_replace('/\n+/', '', $ret_query));
return $ret_query;
}
/*
make_json usage
If $result is a SELECTion, the calling function is responsible to call
php_entity_encode first.
How to access once returned to the .js file and transformed into a
JSON object with JSON.parse():
The query executed:
<JSON object>.query
The Game ID being operated on:
<JSON object>.game_id
The time of execution:
<JSON object>.time
The return value of an UPDATE or INSERT:
<JSON object>.result
The values from a SELECT:
<JSON object>.result[<index>].<field>
*/
function make_json($query, $game_id, $time, $result)
{
$ret_query = query_trim($query);
$ret_query = json_encode($ret_query);
$ret_string = '{"query":' . $ret_query .
',"game_id":"' . $game_id . '"' .
',"time":"' . $time . '"' .
',"result":' . $result . '}';
return $ret_string;
}
?>