0
votes

I've created a HTML ZIP code select menu. I've imported a zipcode sql file to my database. ZIP code options are populated using php and mysql select query. Everything works fine. But it's taking too long to load. There are 42741 zips in US.Is there any way to reduce its loading time?

Code is given below..

<select name="zips[]">
    <option value=""></option>
    <?php
        require("db/dbconfig.php");
        $result=mysql_query("SELECT * FROM `zipcodes`");
        while($row=mysql_fetch_assoc($result)){
    ?>
    <option value="<?php echo $row['ZIP'];?>"><?php echo $row['ZIP'];}?></option>
    <?php    
    ?>

1
Take a look at autocomplete - Naruto
Try to select only ZIP instead of select all fields, i.e: result=mysql_query("SELECT ZIP FROM zipcodes");. and make index on ZIP field. BTW, I personally agree with @Quentin and I believe getting all 42741 row at once in not a good idea - Samiul Amin Shanto
If you give people a select list of ~42000 items nobody is going to bother looking through it to find the one they want. But each row is ~36 bytes, so you drop down list is ~1.3mb. Hope no users are still on a 56k modem! Further point is that you loop around these ~42000 items and break out of php and back into html repeatedly for each one. This is going to be quite a bit slower than just doing it all in php with an echo statement. Also worth adding an order clause to the select, to at least get the ZIP codes back in a useful order. - Kickstart

1 Answers

4
votes

Just let people type the zip code they want. It's a lot faster and easier then picking it from a list and you won't need to send a copy of the list every time the page loads.

Check the zip code against the list of acceptable ones after the data has been submitted.