Short + Long Address Formatting From Address Information


/ Published in: PHP
Save to your folder(s)

Assumes that the record has a `name` field which represents the name of the location and always has a defined two character `country` field.


Copy this code and paste it in your HTML
  1. public function __get($key) {
  2. if($key == 'short_address') {
  3. // this is tricky b/c the map is international
  4. // if the location is US based then we want to display an identifier similiar to: St. Joe's in Downingtown PA
  5. // if the location is non-USA based (and either city or providence is not available) then we want to display: St. Joe's in Downingtown USA
  6.  
  7. return $this->name.' in '.$this->city.' '.$this->providence.
  8. (empty($this->city) || empty($this->providence) ? ' '.$this->country : '');
  9. } else if($key == 'full_address') {
  10. // target format for US based addresses:
  11. // 300 Road Street
  12. // Town PA 19335
  13. // target format for non-US based addresses
  14. // address1
  15. // City Providence Postal Code Country
  16.  
  17. // the trims handle edge cases when there is not enough information to propertly display an address
  18.  
  19. if($this->country == 'US') {
  20. return trim(trim($this->address1."\n".$this->address2)."\n".$this->city)." ".$this->providence." ".$this->postal_code;
  21. } else {
  22. return $this->address1."\n".$this->address2."\n".$this->city." ".$this->providence." ".$this->postal_code." ".$this->country;
  23. }
  24. }
  25.  
  26. return parent::__get($key);
  27. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.