Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Mar 2009
    Posts
    107
    Thanks
    15
    Thanked 0 Times in 0 Posts

    PHP Postcode Search - Help Needed

    Hey Guys,

    I need to develop a postcode search...

    Items in the database will have a location or postcode tagged, E.G London and when you type in your postcode it will bring up that item if you are within say 10 miles of that location.

    I have no idea where to start with this and I couldn't find a lot on Google.

    I was hoping somebody might be able to point me in the right direction?

    Many Thanks in advance!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Doing this isn't a small task.
    To do this, you need to provide the polygon co-ordinate bounds (or at minimum a set of lat/long for a single point, preferably the middle, of the postal code zone) of each of the postal codes. You then calculate the radial bounds of a provided input, and go from there. This isn't a simple task, a good mathematician could help more on this. You can find an algorithm here: http://stackoverflow.com/questions/4...hin-range-of-y
    Also, you'd never use PHP for this as it would be too slow. Small country would be alright, but would grow exponentially with every country added.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,036
    Thanks
    2
    Thanked 316 Times in 308 Posts
    Another way that I don't see suggested much is to just use a bounding box, rather than a radius, to quickly get a gross lists of results, then if you need to eliminate the points that are not within the radius, you can do that as a separate step on the smaller set of data the bounding box found.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    Quote Originally Posted by Fou-Lu View Post
    Also, you'd never use PHP for this as it would be too slow. Small country would be alright, but would grow exponentially with every country added.
    the database would be doing the work not PHP ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Thats right. Uh, perhaps I should have been a little less ambiguous.
    I mean that the calculations should be handled by the dbms; PHP can still initiate and handle the results, but you wouldn't want to calculate them using PHP since you'd need a large dataset. That said, at least where I am there are rules that govern the postal codes, so if you know the rules you can make really small search sets, then either language is sufficient to use for the calculations.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,974
    Thanks
    15
    Thanked 229 Times in 229 Posts
    This would be a formidable task for little php if you used a brute force method, exceeding both memory and timeout settings. What you could do is make up files, with arrays for each postcode that would have as file names the postcode, L.php for Liverpool, etc, and as elements in the array the full postcode addresses, along with position information. You would also need an array to include neighboring postcodes, so they could be checked against. A lot of preparation, it might not even be possible to do it all with php, but in the end it would make it possible to achieve something that works.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #7
    Regular Coder
    Join Date
    Mar 2009
    Posts
    107
    Thanks
    15
    Thanked 0 Times in 0 Posts
    This seems over my head completely , And I have very little time to achieve this in.

    I did find this:
    http://www.postcodesearchscript.co.uk/

    However the demo doesn't actually work and pulls up a load of random results.. Anybody heard of this before?

  • #8
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,974
    Thanks
    15
    Thanked 229 Times in 229 Posts
    On this page http://www.doogal.co.uk/PostcodeDownloads.php You can download the postcode area files, which represent chunks of code that php could probably deal with. Just for ducks I DLed the one for Liverpool, it was nearly 20,000 lines and 8mb.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #9
    Regular Coder
    Join Date
    Mar 2009
    Posts
    107
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DrDOS View Post
    On this page http://www.doogal.co.uk/PostcodeDownloads.php You can download the postcode area files, which represent chunks of code that php could probably deal with. Just for ducks I DLed the one for Liverpool, it was nearly 20,000 lines and 8mb.
    I have just downloaded the Liverpool one myself and went through and checked, it seems pretty thorough, missing some postcodes once into 4 characters etc, but I'm thinking I could maybe do something using just the first 2-3 characters of each area postcode, wont be as effective, but should definitely do the job!

  • #10
    Regular Coder
    Join Date
    Mar 2009
    Posts
    107
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by firepages View Post
    the database would be doing the work not PHP ?

    I have just downlaoded a CSV file , 20,000 lines long for chester postcodes...If I imported a collection of these CSV files into the database totaling aroun 400,000 lines long and then used php to search for ch7 , would this be very slow running?

  • #11
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,974
    Thanks
    15
    Thanked 229 Times in 229 Posts
    Quote Originally Posted by IamHe View Post
    I have just downlaoded a CSV file , 20,000 lines long for chester postcodes...If I imported a collection of these CSV files into the database totaling aroun 400,000 lines long and then used php to search for ch7 , would this be very slow running?
    To be honest, I haven't gotten into databases, someone else may be able to tell you. However, I opened the Liverpool file with another editor, mEdit, and find that it's actually 42,000 lines long. I'm on Linux, which has the BASH shell, along with SED, and the while red line function, and it could chew through all that code and reformat it to something easier for php or a DB to deal with without any trouble. It could convert it to another csv with only the data you need for instance.

    To me the Liverpool file seemed pretty much complete. It's so large it will tax an ordinary page editor.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •