I have already written this PHP script many times, but I have rewritten it because of some changes in the data format provided by the National Bureau of Statistics. The problem involves both PHP and, at the same time, MySQL is also involved, and it is very suitable for interview questions. Such questions often reflect the basic quality of job seekers. Preparations: download the latest administrative divisions of counties and above
I have already written this PHP script many times, but I have rewritten it because of some changes in the data format provided by the National Bureau of Statistics. The problem involves both PHP and, at the same time, MySQL is also involved, and it is very suitable for interview questions. Such questions often reflect the basic quality of job seekers. Preparations: download the latest administrative divisions of counties and above
I have already written this PHP script many times, but I have rewritten it because of some changes in the data format provided by the National Bureau of Statistics. The problem involves both PHP and, at the same time, MySQL is also involved, and it is very suitable for interview questions. Such questions often reflect the basic quality of job seekers.
Preparations: Download and save the latest administrative code in counties and counties, and save the code as a data.txt file. Please note that if the format is incorrect during the copy and paste process, try to paste the copied content to Word first, WPS and other software, and then copy and paste it into a text file.
The latest administrative code for counties and above
Note: For data of different versions, full-width spaces and half-width spaces may be mixed.
Create a MySQL table first:
Note that the character set of the table must be consistent with that of the file.
CREATE TABLE IF NOT EXISTS `region` ( `id` int(10) unsigned NOT NULL, `parent_id` int(10) unsigned NOT NULL, `name` varchar(20) NOT NULL, PRIMARY KEY (`id`), KEY `parent_id` (`parent_id`)) ENGINE=InnoDB;
Supplement: better storage of Hierarchical Data: Storing Hierarchical Data in a Database Article.
Then write the PHP script:
Verify that the content of the data.txt file is valid, and the Code itself is not strictly verified.
PDO::ERRMODE_EXCEPTION,);$dbh = new PDO($dsn, $username, $password, $options);$sth = $dbh->prepare(' INSERT INTO region (id, parent_id, name) VALUES (:id, :parent_id, :name)');$parent = array(0);$handle = fopen('data.txt', 'r');while (!feof($handle)) { $row = trim(str_replace(' ', ' ', fgets($handle))); if (!preg_match('/^(\d+)\s+(.+)$/', $row, $matches)) { continue; } list($row, $id, $name) = $matches; $level = strlen(preg_replace('/(00){1,2}$/', '', $id)) / 2; $parent_id = $parent[$level - 1]; $parent[$level] = $id; $sth->bindValue(':id', $id, PDO::PARAM_INT); $sth->bindValue(':parent_id', $parent_id, PDO::PARAM_INT); $sth->bindValue(':name', $name); $sth->execute();}fclose($handle);?>
Note: The level variable can be introduced to eliminate the annoying "if/else" statement in the program.
......
With the official administrative division code, coupled with a private pure IP database, it is more perfect.
Original article address: Use PHP to parse the administrative division code. Thank you for sharing it.