Create Country> Status> City Lists in WordPress

I was tasked with creating a dynamic list of views for a WordPress page. I will have a list of countries, and then, when a country is selected, the next list will be filled with the states in that country, and then in the next menu with cities in this state.

I could not find a final solution on this issue, I searched around the world.

Does anyone know how to implement this, or where can I find it in a plugin, maybe?

Thanks.

+4
source share
4 answers

Too much trouble ....
here...

function datak() { $url = 'http://api.sba.gov/geodata/city_links_for_state_of/tx.json'; $crl = curl_init($url); curl_setopt($crl, CURLOPT_HEADER, 0); curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1); $rtn = curl_exec($crl); $srn = json_decode($rtn); $nmb=0; echo "<select name='state' id='state'><option value='-1'>Choose a state</option>"; while( $nmb < count($srn) ) { echo '<option value="' . $srn[$nmb]->name . '">' . $srn[$nmb]->name . ' - ' . $srn[$nmb]->primary_longitude . ' - ' . $srn[$nmb]->primary_latitude . "</option>" ; $nmb++; } echo "</select>"; curl_close($crl); } 

It needs some cleaning and ordering - there are duplicate records. Change the status prefix in the url and if you want, you can set the file type in xml, but you will have to transcode the procedure.

Let someone else do datakeeping. Just give users content.

Got it here .

Hope this helps. Isme

+2
source

I don’t know any plugins that have exactly what you need, but if I personally created it, I first looked at this information when filling out the database with all the information for each city.

Information will be organized as follows:

 --------------------------------------------------------------------------------- | Country | State | Province | City | Latitude | Longitude | --------------------------------------------------------------------------------- 

and each city is its own row in the database. Latitude and longitude will be optional, but recommended.

After that, it would be useful to use AJAX to populate drop-down lists on the fly. Basically, you can use jQuery (recommended for this task, since we don’t want to reinvent the wheel) to send a GET or POST request to a separate PHP script. This file will run the SQL query (be sure to avoid the mysql_ * functions as they are deprecated. See PDO for a better alternative) and check the values ​​that are passed to it in the database.

I would recommend populating your database with some basic information. Perhaps set it up for several states and cities, and then study the resources that I gave you to get a working proof of the concept.

If you run into any stumbling blocks, feel free to open another question so that we can help you.

Good luck.

0
source

I found an easy way to display a list of cities of country countries for the whole world. Just add HTML and javascript to your template

 <select name="country" class="countries" id="countryId"> <option value="">Select Country</option> </select> <select name="state" class="states" id="stateId"> <option value="">Select State</option> </select> <select name="city" class="cities" id="cityId"> <option value="">Select City</option> </select> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="http://lab.iamrohit.in/js/location.js"></script> 

https://github.com/hiiamrohit/Countries-States-Cities-database

0
source

Source: https://habr.com/ru/post/1438531/


All Articles