Skip to content

Instantly share code, notes, and snippets.

@alfasado
Created March 21, 2013 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfasado/5210499 to your computer and use it in GitHub Desktop.
Save alfasado/5210499 to your computer and use it in GitHub Desktop.
Find the address from the zip code using PowerCMS's CustomObject.
<?php
global $app;
$q = $app->param( 'q' );
$q = $app->db()->escape( $q );
$q = preg_replace( '/[^0-9]/', '', $q );
if ( $q ) {
$terms = array( 'name' => $q,
'class' => 'postalcode' );
$extra = array( 'limit' => 1 );
$postalcode = $app->load( 'CustomObject', $terms, $extra );
if ( $postalcode ) {
$arr = array( 'zipcode' => $postalcode->name,
'address' => $postalcode->body,
'status' => '200' );
} else {
$arr = array( 'zipcode' => $q,
'address' => '',
'status' => '404' );
}
$json = json_encode( $arr );
$app->send_http_header( 'application/json; charset=UTF-8', time(), strlen( $json ) );
echo $json;
exit();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment