PHP inserts JSON-format provincial/municipal level 2 data into the database

Source: Internet
Author: User
In many cases, the provincial/municipal (district) level-2 linkage drop-down menus, such as address information and weather API calls, are used during Website Development. They are also universal, A warehouse is used everywhere. this method has been mentioned in the previous articles in Nongfu Manor. However, the provincial and municipal information found on the Internet is poor. many cities at the county level are listed in the district city, and will be deleted later, it was quite troublesome. when I tested the Weather API, I found a better json format.

In many cases, the provincial/municipal (district) level-2 linkage drop-down menus, such as address information and weather API calls, are used during Website Development. They are also universal, A warehouse is used everywhere. this method has been mentioned in the previous articles in Nongfu Manor. However, the provincial and municipal information found on the Internet is poor. many cities at the county level are listed in the district city, and will be deleted later, it was quite troublesome. when I tested the Weather API, I found a better province and city information in json format. here I will list the relevant code as follows, hoping to help you:

First, download the file sxinfo.txt, which contains province and city information in json format.

Then copy the json data in the file to the corresponding location in the code below (or you can read the province and city information from the file stream to the string for processing, which will not be repeated here):

 BeginTransaction (); foreach ($ areas as $ area): // echo 'Province/Municipality: '. $ area ['Province'].'
'; $ Province = $ area ['Province']; $ stmt = $ pdo-> prepare ("insert into province ('name') VALUES (: name )"); $ stmt-> bindParam (': name', $ province); if ($ stmt-> execute () {$ cities = $ area ['city']; foreach ($ cities as $ city) {// echo '| --- city/District/Autonomous Prefecture :'. $ city ['code']. '-'. $ city ['City name'].'
'; $ Cid = $ city ['code']; $ name = $ city ['City name']; // query the province id [the name in the province table is unique] $ query = $ pdo-> prepare ("SELECT id FROM province WHERE name = '". $ province. "'"); $ query-> execute (); // Obtain the province id $ pid = $ query-> fetchColumn (); $ stmt2 = $ pdo-> prepare ("insert into city ('id', 'name', 'p _ id') VALUES (: id,: name,: pid) "); $ stmt2-> bindParam (': name', $ name); $ stmt2-> bindParam (': pid ', $ pid ); $ stmt2-> bindParam (': ID', $ cid); if ($ s Tmt2-> execute (): continue; else: echo 'Insert city '. $ name.' failed! '; // If insertion fails, perform the rollBack operation $ pdo-> rollBack (); $ pdo = null; return; endif ;}} else {echo "insert province ". $ province. "failed! "; // If insertion fails, perform the rollBack operation $ pdo-> rollBack (); $ pdo = null; return;} endforeach; // if all data is inserted successfully, perform the submit operation $ pdo-> commit (); $ pdo = null;?>

Now, execute the above code to insert the province and city information into your database.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.