forked from pfinal/city
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-json.php
58 lines (44 loc) · 1.27 KB
/
load-json.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
<?php
include __DIR__ . "/vendor/autoload.php";
$json = file_get_contents('./pca-code.json');
$all = json_decode($json, true);
$config = [
'host' => '127.0.0.1',
'database' => 'test',
'username' => 'root',
'password' => 'root',
];
$db = new \PFinal\Database\Builder($config);
foreach ($all as $p) {
if (!isset($p['children'])) {
echo 'error 1';
exit;
}
//省
$data = ['id' => $p['code'], 'name' => $p['name'], 'parent_id' => ''];
if (!$db->table('region')->insert($data)) {
echo 'db error 1';
exit;
}
echo '<div style="color:red">' . $p['code'] . ' ' . $p['name'] . '</div>';
foreach ($p['children'] as $c) {
//市
$data = ['id' => $c['code'], 'name' => $c['name'], 'parent_id' => $p['code']];
if (!$db->table('region')->insert($data)) {
echo 'db error 2';
exit;
}
if (!isset($c['children'])) {
echo 'error 2';
exit;
}
//区
foreach ($c['children'] as $a) {
$data = ['id' => $a['code'], 'name' => $a['name'], 'parent_id' => $c['code']];
if (!$db->table('region')->insert($data)) {
echo 'db error 3';
exit;
}
}
}
}