postcode finder

This is a discussion on "postcode finder" within the PHP Forum section. This forum, and the thread "postcode finder are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Feb 7th, 2008, 10:57
Reputable Member
Join Date: Dec 2005
Location: scotland
Age: 27
Posts: 160
Thanks: 1
Thanked 0 Times in 0 Posts
postcode finder

Trying to setup a simple postcode search in a site.

Basically, I want the user to enter their postcode into a form which then will check to see if that area is available.

Eg:

User enters : fa21 2e3


I want to check if the fa21 matchesa list of postcode which range from fa1 to fa39.

How is this possible? thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Feb 7th, 2008, 13:52
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: postcode finder

Form:
HTML: Select all
<form action="handle.php">
<input type="text" name="postcode" />
<input type="submit" />
</form>
PHP: Select all

$pcode $_REQUEST['postcode'];
$validpcodes = array('np23 nf3''nog2 0od'); // add more valid postcodes
if(in_array($pcode$validpcodes)) 
{
// is valid

else 
{
//not valid

Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Feb 7th, 2008, 14:26
Daniel's Avatar
Elite Veteran
Join Date: Sep 2006
Location: The Kingdom of Rabbits
Age: 21
Posts: 2,051
Blog Entries: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Re: postcode finder

How would that check if it was available?

You'd need a database and you'd want to get it to check against the database.
Last Blog Entry: Assassin's Creed (Nov 22nd, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Feb 7th, 2008, 15:49
Reputable Member
Join Date: Dec 2005
Location: scotland
Age: 27
Posts: 160
Thanks: 1
Thanked 0 Times in 0 Posts
Re: postcode finder

i have got a databse with lots of postcode, however how do i query on a few?

thanks guys
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Feb 7th, 2008, 16:02
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: postcode finder

PHP: Select all

$validpcodes = array();
//connect to db
$result mysql_query("SELECT postcode FROM postcodetable");
while(
$arr mysql_fetch_assoc($result))

$validpcodes[] = $arr['postcode'];

Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Feb 7th, 2008, 18:19
Reputable Member
Join Date: Dec 2005
Location: scotland
Age: 27
Posts: 160
Thanks: 1
Thanked 0 Times in 0 Posts
Re: postcode finder

ok so i've got this far, but i must be mixing up the variable, can some tell me what i'm doing wrong
PHP: Select all

<?php

    
$validpcodes 
= array();
//connect to db
function get_db_connection(){
      
//connection parameters
        
$db_host '???;
        $db_userid = '
???';
        $db_password = '
???';
        $db_default_database = '
??';
        
      //throw fatal error if couldn'
t connect to mysql
        
if (! @mysql_connect($db_host$db_userid$db_password) ){
            
trigger_error('get_db_connection(): mysql_connect failed - '.mysql_error(), E_USER_ERROR);
            echo(
'get_db_connection(): mysql_connect failed - '.mysql_error());
        }

      
//throw fatal error if couldn't select correct (DEFAULT) database
        
if (! @mysql_select_db($db_default_database) ){
            
trigger_error('get_db_connection(): mysql_select_db failed - '.mysql_error(), E_USER_ERROR);
            echo(
'get_db_connection(): mysql_select_db failed - '.mysql_error());
        }
    }
    
$result mysql_query("SELECT outcode FROM geg_postcode_table");
while(
$arr mysql_fetch_assoc($result))

$validpcodes[] = $arr['outcode'];

$pcode $_REQUEST['postcode'];
$pcode = array($arr); // add more valid postcodes
if(in_array($pcode$arr)) 
{
echo 
'yes';

else 
{
echo 
'no';
}
and getting these errors
Quote:
Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in ....coverage.php on line 26

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in ...coverage.php on line 26

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in ..coverage.php on line 27

Warning: in_array() [function.in-array]: Wrong datatype for second argument in ...coverage.php on line 33
no

Last edited by alexgeek; Feb 27th, 2008 at 20:35.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Feb 7th, 2008, 18:29
Marc's Avatar
Staff Manager

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Posts: 1,798
Thanks: 0
Thanked 17 Times in 17 Posts
Re: postcode finder

PHP: Select all

 $db_host = ???; 

you need to close the '

like:

PHP: Select all

  $db_host '???'
__________________
Marc
Staff Manager - Webforumz.com


Want to be a moderator? PM me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Feb 7th, 2008, 18:32
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: postcode finder

Also this:
PHP: Select all

$pcode $_REQUEST['postcode'];
$pcode = array($arr); // add more valid postcodes 
Should just be:
PHP: Select all

$pcode $_REQUEST['postcode']; 

Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Feb 7th, 2008, 19:00
Reputable Member
Join Date: Dec 2005
Location: scotland
Age: 27
Posts: 160
Thanks: 1
Thanked 0 Times in 0 Posts
Re: postcode finder

1st prob was just a mistake when i was taking out my database pw, etc.

fixed the 2nd one but still no joy, thought it was a connection problem but tested database with another script and it worked fine

these lines seem to be throwing up the probs

Quote:
$result = mysql_query("SELECT outcode FROM geg_postcode_table");

while($arr = mysql_fetch_assoc($result))

if(in_array($pcode, $arr))
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Feb 7th, 2008, 19:08
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: postcode finder

Supposed to be:
PHP: Select all

if(in_array($pcode$validpcodes)) 

Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Feb 7th, 2008, 19:37
Reputable Member
Join Date: Dec 2005
Location: scotland
Age: 27
Posts: 160
Thanks: 1
Thanked 0 Times in 0 Posts
Re: postcode finder

fixed the last 2 probs but still having truble connecting, really strange
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Feb 7th, 2008, 20:00
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: postcode finder

Post errors.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Feb 7th, 2008, 20:23
Reputable Member
Join Date: Dec 2005
Location: scotland
Age: 27
Posts: 160
Thanks: 1
Thanked 0 Times in 0 Posts
Re: postcode finder

just foubd out that the database i was accessing was incomplete and have had to re-think (temp) my plans, tryin to ue your origional script alex but no matter what postcode i enter it always comes back saying no?
PHP: Select all

<?php

$pcode 
$_REQUEST['postcode'];
$validpcodes = array('G1''G11''G12''G13''G14''G15''G2''G20''G21''G22''G23''G3''G31''G32''G33''G34''G4''G40''G41''G42''G43''G44''G45''G46''G5''G51''G52''G53''G60''G61''G62''G63''G64''G65''G66''G67''G68''G69''G71''G72''G73''G74''G76''G77''G78''G81''G82''G83''G84', ); // add more valid postcodes
if(in_array($pcode$validpcodes)) 
{
// is valid
echo 'yes';

else 
{
//not valid
echo 'no';

?>
any idea why?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #14  
Old Feb 8th, 2008, 19:10
Reputable Member
Join Date: Dec 2005
Location: scotland
Age: 27
Posts: 160
Thanks: 1
Thanked 0 Times in 0 Posts
Re: postcode finder

sorted, was case sensitive!! doh, easy fix. cheers again Alex
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #15  
Old Feb 8th, 2008, 19:22
Marc's Avatar
Staff Manager

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Posts: 1,798
Thanks: 0
Thanked 17 Times in 17 Posts
Re: postcode finder

Haha.. Yeah.. PHP is case-senSitIvE
__________________
Marc
Staff Manager - Webforumz.com


Want to be a moderator? PM me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #16  
Old Feb 8th, 2008, 19:28
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: postcode finder

Glad you have that sorted
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools