-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathregion2sql.php
More file actions
27 lines (20 loc) · 1.08 KB
/
region2sql.php
File metadata and controls
27 lines (20 loc) · 1.08 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
<?php
$sql = "DROP TABLE IF EXISTS `region`;
CREATE TABLE `region` (
`code` int(11) NOT NULL COMMENT '行政区划代码',
`name` varchar(50) NOT NULL COMMENT '地区名称',
`level` tinyint NOT NULL COMMENT '地区级别:1.省 2.市 3.区(县)',
`parent` int(11) NOT NULL COMMENT '上级行政区划代码',
PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='地区表';\n";
$data = json_decode(file_get_contents('./region.json'), 1);
foreach ($data['provinces'] as $province) {
$sql .= "INSERT INTO `region`(`code`, `name`, `level`, `parent`) VALUES('{$province['code']}', '{$province['name']}', '1', '0');\n";
foreach ($province['cities'] as $city) {
$sql .= "INSERT INTO `region`(`code`, `name`, `level`, `parent`) VALUES('{$city['code']}', '{$city['name']}', '2', '{$province['code']}');\n";
foreach ($city['areas'] as $area) {
$sql .= "INSERT INTO `region`(`code`, `name`, `level`, `parent`) VALUES('{$area['code']}', '{$area['name']}', '3', '{$city['code']}');\n";
}
}
}
file_put_contents('./region.sql', $sql);