1

Ok so I have three tables which contains list World's countries their states and their cities for my registration form. The problem is that the list of the cities is too huge. It contains 48,314 entries in total. So my site is getting hanged and the browser is showing messages to stop script. I am using mozilla for browser purpose.

This is the code I am using to get the cities, states and countries:

$country = "SELECT * FROM countries";
$country = $pdo->prepare($country);
$country->execute();

$state = "SELECT * FROM states";
$state = $pdo->prepare($state);
$state->execute();

$city = "SELECT * FROM cities";
$citq = $pdo->prepare($city);
$citq->execute();

This is my jQuery code:

$(document).ready(function () {
    $("#country").change(function() {
      if ($(this).data('options') == undefined) {
        $(this).data('options', $('#state option').clone());
      }
      var id = $(this).val();
      var options = $(this).data('options').filter('[value=' + id + ']');
      $('#state').html('<option value="">Select State</option>').append(options);
    });
    $("#state").change(function() {
      if ($(this).data('options') == undefined) {
        $(this).data('options', $('#city option').clone());
      }
      var id = $(this).val();
      var options = $(this).data('options').filter('[value=' + id + ']');
      $('#city').html('<option value="">Select City</option>').append(options);
    });
});

This is my HTML:

<select name="country" id="country">
<option value="">Select Country</option>
<?php while($i = $country->fetch()){ extract($i); ?>
<option value="<?php echo $id; ?>"><?php echo $name; ?></option>
<?php } ?>
</select>

<select name="state" id="state">
<option value="">Select State</option>
<?php while($j = $state->fetch()){ extract($j); ?>
<option value="<?php echo $country_id; ?>" data="<?php echo $id; ?>"><?php echo $name; ?></option>
<?php } ?>
</select>

<select name="city" id="city">
<option value="">Select City</option>
<?php while($k = $citq->fetch()){ extract($k); ?>
<option value="<?php echo $id ; ?>" data="<?php echo $state_id; ?>"><?php echo $name ; ?></option>
<?php } ?>
</select>

Now can anyone please help me getting a solution as to how I can load it completely smoothly without getting my site hanged whenever the page is refreshed?

  • 1
    consider using pagination – Funk Forty Niner Oct 01 '16 at 16:55
  • `extract($i)` in the global scope **YUK** – RiggsFolly Oct 01 '16 at 16:57
  • 1
    Plus, you could probably use one query and joining those tables and if they have anything in common, while using proper indexing. – Funk Forty Niner Oct 01 '16 at 16:57
  • 1
    **Why in the name of all thats holie** Would ANYBODY want to see 48,000 cities in a dropdown list. **It is totally UNUSABLE** Have you ever been presented with a dropdown that large? _It would role all the way down the page and over the desk and down to the floor and then off into the next room_ – RiggsFolly Oct 01 '16 at 16:58
  • @RiggsFolly it makes for a lot of preparing/querying for sure. – Funk Forty Niner Oct 01 '16 at 16:58
  • @Fred-ii- Although they are all in seperate dropdowns, so maybe not – RiggsFolly Oct 01 '16 at 16:59
  • http://stackoverflow.com/questions/109232/what-is-the-best-way-to-paginate-results-in-sql-server – Stefan Oct 01 '16 at 17:00
  • So if I select country = 'France' you will show the cities of all your countries? Anyone has ever used the 'where' statement? – Steve Oct 01 '16 at 17:02
  • Why don't you load the cities _after_ the country got selected? – Jeff Oct 01 '16 at 17:03
  • let me clear.. If you select France is 1st dropdown then in 2nd dropdown you will be shown all the states in France then when you select one of the states from the 2nd dropdown then it will show all the cities in THAT STATE in the 3rd dropdown.. is it clear? its as simple as that.. –  Oct 01 '16 at 17:06
  • Also remember, that the Country-State-City pattern does not work in many countries the way it does in US. (What state is Paris in??) – Jeff Oct 01 '16 at 17:06
  • Do a google on "PHP and AJAX" there is quite a bit to learn I am afriad – RiggsFolly Oct 01 '16 at 17:06
  • That is not clear from the code you show – RiggsFolly Oct 01 '16 at 17:09
  • but I am getting my current results from this.. its ok till I get states as the list of states is less but when it comes to city my site crashes.. –  Oct 01 '16 at 17:12

1 Answers1

0

You could load the states and cities dynamically once the "parent" selection is made. This would reduce the amount of data. No clear code because I think you know what you are doing, but the idea:

-> [html] select 
-> [js] onChange call php with ajax
-> [php] SQL select states where country="chosencountry" 
-> [js] update form/selectbox

EDIT: (code)

JS:

<script>
        function BuildSelectbox(job,parent) {

                try { req = window.XMLHttpRequest?new XMLHttpRequest(): 
                      new ActiveXObject("Microsoft.XMLHTTP"); 
                     } catch (e) {  /* No AJAX Support */ }

                req.open('get','subselects.php?job='+job+'&parent='+parent);

                /* let the php echo the resultvalue */

                req.onreadystatechange = function() {
                 handleResponse(div);
                };
                req.send(null);
        }

        function handleResponse(div) {


               if ((req.readyState == 4) && (req.status == 200)) {
                   document.getElementById(job).value=req.responseText;
               }
        }
</script>

PHP part: (subselects.php)

<?
if ($_GET["job"]=="states") {

// assuming there is a key country in states
$state = "SELECT * FROM states where country=".$_GET["parent"];
$state = $pdo->prepare($state);
$state->execute();

} else {

// assuming there is a key state in cities
$city = "SELECT * FROM cities where state=".$_GET["parent"];
$citq = $pdo->prepare($city);
$citq->execute();
}

// echo the whole selectbox
echo '<select id="'.$_GET["job"].'">';
// put the option loop from your queryresult here
echo '</select>';    

?>

HTML:

<div id="countries" onChange="BuildSelectbox('states',this.selectedIndex);>
<select name="country" id="country">
<option value="">Select Country</option>
<?php while($i = $country->fetch()){ extract($i); ?>
<option value="<?php echo $id; ?>"><?php echo $name; ?></option>
<?php } ?>
</select>
</div>

<div id="states"></div>
<div id="cities"></div>

This dynamically generates full selectboxes and puts them into the empty divs "states and "cities". Of course you need to output the selectbox in the php code. Parent of states is country and parent of cities is states. Hope this explains it.

  • _No clear code because I think you know what you are doing_ **Really** – RiggsFolly Oct 01 '16 at 17:04
  • I know what I am doing but I am poor in AJAX so I used jQuery here to help me to some extent.. clear code would have been useful.. –  Oct 01 '16 at 17:09
  • Okay, I´ll add. – Gemelo Molinero Oct 01 '16 at 17:12
  • let me clear.. If you select France is 1st dropdown then in 2nd dropdown you will be shown all the states in France then when you select one of the states from the 2nd dropdown then it will show all the cities in THAT STATE in the 3rd dropdown.. is it clear? its as simple as that.. –  Oct 01 '16 at 17:21
  • plus 1 i will give for your effort of trying to help me.. answer will be accepted if it works.. –  Oct 01 '16 at 17:41
  • let me know if you need further assistance – Gemelo Molinero Oct 02 '16 at 09:21
  • i modified your code a bit and it worked flawlessly.. thanks for the idea :) –  Oct 03 '16 at 13:09