In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how php realizes provincial and municipal query". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
php provincial query method: 1, design a data table;2, according to the query id to find the corresponding parent_id;3, through the "GetCityInfo(searstr, cityselect) function {...} "The code can be used to query provincial and municipal areas.
Operating environment: Windows 7 system, PHP 7.1 version, Dell G3 computer.
PHP realizes three-level linkage query of provinces, cities and counties (districts)
linkage query
What is linkage query? In fact, this thing is very common in life, such as online shopping.(Almost everyone has used this). When shopping online, we need to fill in the order information. There is an item in the information that needs to fill in the address. Some websites use linkage query, which saves the time for users to fill in the order. At this time, linkage query comes in handy. For example, when we select Hebei Province in the provincial drop-down box, the corresponding urban area needs to change the information in it. Load the corresponding urban area into the drop-down box and paste a picture.
database design
Design a table, and then this table loop query itself, according to the query id to find the corresponding parent_id, you can query the desired results, the last two fields are redundant fields, optional, according to their project requirements
The statement to create the database is as follows, the database is MySQL database
CREATE TABLE `china_area` ( `id` int(11) NOT NULL, `parent_id` int(11) NOT NULL DEFAULT '0', `code` varchar(10) NOT NULL, `name` varchar(191) NOT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Simply insert some data, complete table data is very large, about three thousand lines, here do not show all the data code, so as not to be flashy, if you want these data, you can go online to find, or your own design, I also uploaded a copy, but need points
INSERT INTO `china_area`(`id`, `parent_id`, `code`, `name`, `created_at`, `updated_at`) VALUES (1, 0, '110000',' Beijing City ', NULL, NULL),(2, 0, '120000',' Tianjin City ', NULL, NULL),(3, 0, '130000',' Hebei Province ', NULL, NULL),(4, 0, '140000',' Shanxi Province ', NULL, NULL),(5, 0, '15000',' Inner Mongolia Autonomous Region ', NULL, NULL),(6, 0, '210000',' Liaoning Province ', NULL, NULL),(7, 0, '22000',' Jilin Province ', NULL, NULL),(8, 0, '23000',' Heilongjiang Province ', NULL, NULL),(9, 0, '310000',' Shanghai City ', NULL, NULL),(10, 0, '32000',' Jiangsu Province ', NULL, NULL),(11, 0, '33000',' Zhejiang Province ', NULL, NULL),(12, 0, '34000',' Anhui Province ', NULL, NULL),(13, 0, '35000',' Fujian Province ', NULL, NULL),(14, 0, '36000',' Jiangxi Province ', NULL, NULL),(15, 0, '37000',' Shandong Province ', NULL, NULL),(16, 0, '410000',' Henan Province ', NULL, NULL),(17, 0, '42000',' Hubei Province ', NULL, NULL),(18, 0, '43000',' Hunan Province ', NULL, NULL),(19, 0, '44000',' Guangdong Province ', NULL, NULL),(20, 0, '45000',' Guangxi Zhuang Autonomous Region ', NULL, NULL),(21, 0, '46000',' Hainan Province ', NULL, NULL),(22, 0, '50000',' Chongqing City ', NULL, NULL),(23, 0, '510000',' Sichuan Province ', NULL, NULL),(24, 0, '52000',' Guizhou Province ', NULL, NULL),(25, 0, '53000',' Yunnan Province ', NULL, NULL),(26, 0, '54000',' Tibet Autonomous Region ', NULL, NULL),(27, 0, '61000',' Shaanxi Province ', NULL, NULL),(28, 0, '62000',' Gansu Province ', NULL, NULL),(29, 0, '63000',' Qinghai Province ', NULL, NULL),(30, 0, '64000',' Ningxia Hui Autonomous Region ', NULL, NULL),(31, 0, '65000',' Xinjiang Uygur Autonomous Region ', NULL, NULL),(32, 0, '71000',' Taiwan Province ', NULL, NULL),(33, 0, '81000',' Hong Kong Special Administrative Region ', NULL, NULL),(34, 0, '82000',' Macao Special Administrative Region ', NULL, NULL),(35, 1, '110100',' Beijing Urban area ', NULL, NULL),(36, 35, '110101',' Dongcheng Area ', NULL, NULL),(37, 35, '110102',' Xicheng District ', NULL, NULL),(38, 35, '110105',' Chaoyang District ', NULL, NULL),(39, 35, '110106',' Fengtai District ', NULL, NULL),(40, 35, '110107',' Shijingshan District ', NULL, NULL),(41, 35, '110108',' Haidian District ', NULL, NULL),(42, 35, '110109',' Mentougou District ', NULL, NULL),(43, 35, '110111',' Fangshan District ', NULL, NULL),(44, 35, '110112',' Tongzhou District ', NULL, NULL),(45, 35, '110113',' Shunyi District ', NULL, NULL),(46, 35, '110114',' Changping District ', NULL, NULL),(47, 35, '110115',' Daxing District ', NULL, NULL),(48, 35, '110116',' Huairou District ', NULL, NULL),(49, 35, '110117',' Pinggu District ', NULL, NULL),(50, 35, '110118',' Miyun Zone', NULL, NULL),(51, 35, '110119',' Yanqing District ', NULL, NULL),(52, 2, '120100',' Tianjin Urban area ', NULL, NULL),(53, 52, '120101',' Heping District ', NULL, NULL),(54, 52, '120102',' Hedong District ', NULL, NULL),(55, 52, '120103',' Hexi District ', NULL, NULL),(56, 52, '120104',' Nankai District ', NULL, NULL),(57, 52, '120105',' Hebei District ', NULL, NULL),(58, 52, '120106',' Hongqiao District ', NULL, NULL),(59, 52, '120110',' Dongli District ', NULL, NULL),(60, 52, '120111',' Xiqing District ', NULL, NULL),(61, 52, '120112',' Jinnan District ', NULL, NULL),(62, 52, '120113',' Beichen District ', NULL, NULL),(63, 52, '120114',' Wuqing District ', NULL, NULL),(64, 52, '120115',' Baodi District ', NULL, NULL),(65, 52, '120116',' Binhai New District ', NULL, NULL),(66, 52, '120117',' Ninghe District ', NULL, NULL),(67, 52, '120118',' Tranquility Zone', NULL, NULL),(68, 52, '120119',' Jizhou District ', NULL, NULL),(69, 3, '130100',' Shijiazhuang City ', NULL, NULL),(70, 3, '130200',' Tangshan City ', NULL, NULL),(71, 3, '130300',' Qinhuangdao City ', NULL, NULL),(72, 3, '130400',' Handan City ', NULL, NULL),(73, 3, '130500',' Xingtai City ', NULL, NULL),(74, 3, '130600',' Baoding City ', NULL, NULL),(75, 3, '130700',' Zhangjiakou City ', NULL, NULL),(76, 3, '130800',' Chengde City ', NULL, NULL),(77, 3, '130900',' Cangzhou ', NULL, NULL),(78, 3, '131000',' Langfang City ', NULL, NULL),(79, 3, '131100',' Hengshui City ', NULL, NULL),(80, 69, '130102',' Chang 'an District', NULL, NULL),(81, 69, '130104',' Qiaoxi District ', NULL, NULL),(82, 69, '130105',' Xinhua District ', NULL, NULL),(83, 69, '130107',' Jingxing Kuangqu ', NULL, NULL),(84, 69, '130108',' Yuhua District ', NULL, NULL),(85, 69, '130109',' Gaocheng District ()', NULL, NULL),(86, 69, '130110',' Deer Spring', NULL, NULL),(87, 69, '130111',' Luanchengqu', NULL, NULL),(88, 69, '130121',' Jingxing County ', NULL, NULL),(89, 69, '130123',' Zhengding County ', NULL, NULL),(90, 69, '130125',' Xingtang County ', NULL, NULL),(91, 69, '130126',' Lingshou County ', NULL, NULL),(92, 69, '130127',' Gaoyi County ', NULL, NULL),(93, 69, ' 130128','Shenze County', NULL, NULL),(94, 69, '130129',' Zanhuang County ', NULL, NULL),(95, 69, ' 130130','Wuji County', NULL, NULL);
Database is roughly like this, through the upper level id and then look up the value corresponding to parent_id to find the corresponding data, circular search, three-level linkage search twice can find the corresponding results, gossip less, the following start to explain the idea.
design ideas
The first drop-down box (i.e. province) is loaded when the page is initialized, so there is no need to load asynchronously. The second (city) and third (county, district) are loaded asynchronously. Then, after the user pulls down the box, send an Ajax request through the js event to obtain the background code. Then, the background searches according to the value of the parent_id passed over (try not to use the field in the database for page transmission). The database query code is as follows
SELECT * FROM `china_area` WHERE parent_id=".$_ GET['precityid']
Then splice the results into json and send it to the foreground, and then splice the results with js.
code implementation
The first drop-down box can be generated directly when the page is loaded. City and He County only need to add the select tag without adding the corresponding data. By default, the drop-down box with no selection is-1
//Reference bootstrap framework and jq library, save code Please select...
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.