/
main.php
111 lines (90 loc) · 3.2 KB
/
main.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
<?php
require_once "connection.php";
define("DIR", 'files/');
//получаем массив с названиями таблиц из БД
function get_tables($conn){
$tables = array();
$query = "SHOW TABLES";
$result = $conn->query($query)->fetch_all(MYSQL_NUM);
foreach($result as $table){
$tables[] = $table[0];
}
return $tables;
}
//получаем названия столбцов из таблиц БД
function get_fields($table, $conn){
$data = array();
$query = "SHOW COLUMNS FROM $table";
$result = $conn->query($query)->fetch_all(MYSQL_NUM);
foreach($result as $field){
$data[] = $field[0];
}
return $data;
}
//получаем данные из БД для json
function get_data_json($table, $conn){
$query = "SELECT * FROM $table";
$result = $conn->query($query)->fetch_all(MYSQL_ASSOC);
return $result;
}
//получаем данные из БД для csv и xml
function get_data($table, $conn){
$query = "SELECT * FROM $table";
$result = $conn->query($query)->fetch_all(MYSQL_NUM);
return $result;
}
//создание файла CSV
function create_csv($table, $conn){
$data_from_table = get_data($table, $conn);
$csv = fopen("files/csv/".$table.".csv", "w+");
$fields = get_fields($table, $conn);
fputcsv($csv, $fields, ";");
foreach ($data_from_table as $value){
fputcsv($csv, $value, ";");
}
fclose($csv);
}
//создание файла JSON
function create_json($table, $conn){
$data_from_table = get_data_json($table, $conn);
$json = fopen("files/json/".$table.".json", "w+");
fwrite($json, json_encode($data_from_table));
fclose($json);
}
//создание файла XML
function create_xml($table, $conn){
//получаем названия столбцов
$fields = get_fields($table, $conn);
//получаем данные
$data_from_table = get_data($table, $conn);
//создаем объект документа
$dom = new DOMDocument('1.0', 'UTF-8');
//создаем корневой элемент по имени таблицы
$catalog = $dom->createElement("catalog");
$dom->appendChild($catalog);
//создаем контейнер для обектов
$entities = $dom->createElement("entities");
$catalog->appendChild($entities);
//создаем и добавляем объекты
foreach($data_from_table as $value){
$entity = $dom->createElement("entity");
$attribute_id = $dom->createAttribute('id');
$attribute_id->value = $value[0];
$entity->appendChild($attribute_id);
for($k = 0, $i = count($fields); $k < $i; $k++ ){
$name_tag = $dom->createElement($fields[$k], $value[$k]);
$entity->appendChild($name_tag);
}
$entities->appendChild($entity);
}
//сохраняем документ
$dom->save("files/xml/".$table.".xml");
}
//скачиваем файл
function download_file($name){
header("Content-Disposition: attachment; filename=".$name."");
header("Content-Type: application/x-force-download; name=".$name."");
header("Content-Length: ".filesize($name));
readfile($name);
exit;
}