Subversion Repositories PEI Public Data

[/] [ajaxlookup/] [PEICivicAddressLookupAJAX.php] - Rev 5

Compare with Previous | Blame | View Log

<?
/**
  * PEICivicAddressLookupAJAX.php - Companion AJAX script to PEICivicAddressLookup.php
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or (at
  * your option) any later version.

  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  * USA
  *  
  * @version 0.1, November 7, 2005 
  * @link http://ruk.ca/wiki/PEI_Civic_Address_Lookup_with_AJAX
  * @author Peter Rukavina <peter@rukavina.net> 
  * @copyright Reinvented Inc., 2005
  * @license http://www.fsf.org/licensing/licenses/gpl.txt GNU Public License
  */

//---------------------------------------
// User Configurable Settings
//---------------------------------------  

// Include User configurable options

include "settings/PEICivicAddressData.inc";

//---------------------------------------
// End of User Configurable Settings
//---------------------------------------  

MYSQL_CONNECT($mysql_host,$mysql_user,$mysql_passwd);
MYSQL_SELECT_DB$mysql_database ) or die( "Unable to select database $mysql_database\n");

// Grab the 'civicaddress' field from the POST data

$lookfor $_POST['civicaddress'];        

// Find out whether a space has been typed yet

$spaceplace strpos($lookfor," ");

// If no space typed yet, then return "Keep typing..." because we don't have enough data yet.

if ($spaceplace === false) {
    print 
"<ul><li>Keep typing...</li></ul>";
}

// Otherwise we've got a space, so we keep going...

else {
    
$street_no substr($lookfor,0,$spaceplace);
    
$street_nm substr($lookfor,$spaceplace+1);

    
// If they haven't typed anything *after* the space, then we *still* need more...

    
if (trim($street_nm) == "") {
        print 
"<ul><li>Keep typing...</li></ul>";
    }

    
// We've got at least a street number and a letter of the street name, which is enough to search on

    
else {

        
// We're only going to search on the first 5 characters of the street name.

        
$street_nm substr($street_nm,0,5);

        
// Build a query

        
$query "SELECT street_no,street_nm,comm_nm from civicaddress where 
                    street_no = '$street_no' and 
                    street_nm like '" 
$street_nm "%' 
                    order by street_nm"
;
    
        
$result MYSQL_QUERY($query);
        
$howmanyrecords MYSQL_NUM_ROWS($result);
        
$currentrecord 0;

        
// The other end of the AJAX pipeline expects an unordered list of results, so we make one...
        
        
$out "<ul>";
        while (
$currentrecord $howmanyrecords) {
            
$street_no trim(MYSQL_RESULT($result,$currentrecord,"street_no"));
            
$street_nm trim(MYSQL_RESULT($result,$currentrecord,"street_nm"));
            
$comm_nm trim(MYSQL_RESULT($result,$currentrecord,"comm_nm"));

            
$out .= "<li>" $street_no " " $street_nm ", " $comm_nm "</li>\n";
            
$currentrecord++;
        }
        
$out .= "</ul>";
        print 
$out;
    }
}
?>

Compare with Previous | Blame | View Log