-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_file.php
81 lines (77 loc) · 2.4 KB
/
export_file.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
<?php
require 'dbconnect.php';
function export_data($export_array, $export_type) {
$file_name = rand();
switch ($export_type) {
case 'json':
$json_file = json_encode($export_array);
$fp = fopen('export_files/'.$file_name.'.json', 'w');
fwrite($fp, $json_file);
fclose($fp);
$return=$file_name.'.json';
break;
case 'xml':
$xml = arrayToXml($export_array);
$xml->asXML('export_files/'.$file_name.'.xml');
$return=$file_name.'.xml';
break;
case 'csv':
$fp = fopen('export_files/'.$file_name.'.csv', 'w');
foreach ($export_array as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
$return=$file_name.'.csv';
break;
}
return $return;
}
function add_activity_info($row, $activities, $conn) {
$activities_check = "AND (";
for ($i=0; $i < count($activities) ; $i++) {
$activities_check.= "(".$activities[$i]. " IS NOT NULL) ";
if($i != count($activities) -1) {
$activities_check.="OR ";
}
}
$activities_check.=")";
$query_2 = sprintf("SELECT * FROM `user_activity` WHERE (userMapData_timestampMs = '%s') $activities_check",
mysqli_real_escape_string($conn, $row["timestampMs"]));
$result_2 = mysqli_query($conn, $query_2);
if(!$result_2){
return false;
}
else {
$final_array =array();
while ($row_2 = mysqli_fetch_assoc($result_2)) {
foreach ($row_2 as $key => $value) {
if($key == 'userMapData_timestampMs' || $key == 'userMapData_userId'){
unset($row_2[$key]);
}
}
//$row_2 += $userId;
$final_array=array_merge($row, $row_2);
}
return $final_array;
}
}
function arrayToXml($array, $rootElement = null, $xml = null) {
$_xml = $xml;
// If there is no Root Element then insert root
if ($_xml === null) {
$_xml = new SimpleXMLElement($rootElement !== null ? $rootElement : '<root/>');
}
// Visit all key value pair
foreach ($array as $k => $v) {
// If there is nested array then
if (is_array($v)) {
// Call function for nested array
arrayToXml($v, $k, $_xml->addChild($k));
}else {
// Simply add child element.
$_xml->addChild($k, $v);
}
}
return $_xml;
}
?>