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.