<?php
// These headers are compulsory, to avoid data cache. Alternatively use a POST or GET with timestamp as parameter from PAX
header("Cache-Control: no-cache");
header("Pragma: nocache");
// Request values
$query = strtolower($_REQUEST['query']);
$searchMode = strtolower($_REQUEST['searchMode']);
// Limited (303) set of suburbs, obviously you would retrieve them all from the database.
$suburb = array(
'Adamstown', 'Allgomera Creek', 'Alumy Creek', 'Araluen North', 'Argenton', 'Argyle', 'Ashville', 'Auburn Vale',
'Audley', 'Balarang', 'Balgowlah', 'Bandon', 'Bannister', 'Bar Beach', 'Bar Point', 'Barrack Heights', 'Barry',
'Baryulgil', 'Batemans Bay', 'Belgravia', 'Belowra', 'Beni', 'Berrigan', 'Bilambil Heights', 'Billimari',
'Birchgrove', 'Bithramere', 'Black Springs', 'Bobs Creek', 'Bocoble', 'Bolton Point', 'Boomoodeerie',
'Booti Booti', 'Boree Creek', 'Bowenfels', 'Bowraville', 'Box Ridge', 'Brockley', 'Brooklana', 'Broughton Vale',
'Brunkerville', 'Brushgrove', 'Bucca Wauka', 'Budgee Budgee', 'Bukkulla', 'Bumbaldry', 'Burragate',
'Busbys Flat', 'Byrock', 'Calarie', 'Caloola', 'Camden Park', 'Carwoola', 'Cassilis', 'Castle Hill',
'Castlecrag', 'Central Macdonald', 'Chester Hill', 'Chinnock', 'Clifton Grove', 'Clovass', 'Coffee Camp',
'Coleambally', 'Colinroobie', 'Como', 'Congo', 'Coniston', 'Cootamundra', 'Copeland', 'Corlette', 'Corndale',
'Cornwallis', 'Crackenback', 'Croppa Creek', 'Crowdy Head', 'Crows Nest', 'Croydon', 'Currawananna', 'Dabee',
'Dalton', 'Dargan', 'Darling Point', 'Denhams Beach', 'Denistone East', 'Dewitt', 'Donnellyville', 'Dooralong',
'Douglas Park', 'Dubbo Grove', 'Dubbo West', 'Duck Creek', 'Dudley', 'Dum Dum', 'Dumaresq Island', 'Dunoon',
'Duranbah', 'Dyraaba', 'East Jindabyne', 'Eden', 'Euberta', 'Eumungerie', 'Euriowie', 'Fernmount', 'Fernvale',
'Flinders', 'Forest Glen', 'Fortis Creek', 'Garah', 'Garra', 'Gemville', 'Georges Hall', 'Gilgai', 'Glen Alice',
'Glen Fergus', 'Glen Oak', 'Glenbawn', 'Glendenning', 'Glenmore', 'Good Hope', 'Goulburn North', 'Gouldsville',
'Grays Point', 'Green Valley', 'Greenleigh', 'Grogan', 'Gunderman', 'Gundy', 'Halton', 'Hanwood', 'Hill Top',
'Hilldale', 'Holgate', 'Hoskinstown', 'Hovells Creek', 'Illabo', 'Ironmungy', 'Islington', 'Jellat Jellat',
'Jeremy', 'Jerrabomberra', 'Jerrara', 'Jerrong', 'Jewells', 'Kain', 'Kangaroo Point', 'Kanimbla', 'Kanwal',
'Karuah', 'Kerrabee', 'Kikiamah', 'Kings Creek', 'Kirkconnell', 'Kogarah Bay', 'Kooragang', 'Kooringal',
'Kulnura', 'Kurri Kurri', 'Lake Cathie', 'Lake Cargelligo', 'Lavadia', 'Leconfield', 'Lidcombe North',
'Lillian Rock', 'Lilyvale', 'Linburn', 'Little River', 'Little Topar', 'Little Wobby', 'Liverpool', 'Long Reef',
'Lower Mangrove', 'Lower Thredbo', 'Luddenham', 'Maimuru', 'Mallowa', 'Manly', 'Maragle', 'Mardi', 'Mascot',
'Maxwell', 'Medowie', 'Merrigal', 'Merriwagga', 'Metford', 'Middle Pocket', 'Milby', 'Mitchell', 'Mondrook',
'Monivae', 'Moore Park', 'Moorong', 'Moparrabah', 'Moppy', 'Morton', 'Morven', 'Moss Vale', 'Moulamein',
'Mount Dee', 'Mount Elliot', 'Mount Keira', 'Mount Saint Thomas', 'Mount View', 'Mount Wilson', 'Mowbray Park',
'Mulloon', 'Mullumbimby Creek', 'Myall Park', 'Myuna Bay', 'Nelson', 'Nerriga', 'Nerrigundah', 'New Buildings',
'Newnes', 'Niangala', 'Noonbinna', 'North Arm Cove', 'North Batemans Bay', 'North Lismore', 'North Yeoval',
'Nowra North', 'Nullica', 'Old Bar', 'Ootha', 'Oswald', 'Oxley Island', 'Oxley Park', 'Oxley Vale',
'Pages Creek', 'Paling Yards', 'Palmdale', 'Pampoolah', 'Penrith', 'Pimlico', 'Pine Clump', 'Pitnacree',
'Port Macquarie', 'Premer', 'Pretty Pine', 'Pucawan', 'Quipolly', 'Raymond Terrace', 'Redhead', 'Reedy Creek',
'Reidsdale', 'Rock Flat', 'Rockdale', 'Saratoga', 'Sassafras', 'Shoalhaven Heads', 'Smithfield West',
'Snakes Plain', 'Spicers Creek', 'St Andrews', 'St Ives', 'Stanbridge', 'Stanford Merthyr', 'Stanhope',
'Stanhope Gardens', 'Stanwell Park', 'Steam Plains', 'Sutherland', 'Sweetmans Creek', 'Tabulam', 'Talmalmo',
'Tara', 'Tenambit', 'Tenterden', 'The Pilliga', 'Thirroul', 'Tilbuster', 'Tilpa', 'Tocumwal', 'Toronto',
'Towac', 'Tumblong', 'Ulladulla', 'Upper Eden Creek', 'Upper Rollands Plains', 'Urangeline', 'Uranquinty',
'Utungun', 'Voyager Point', 'Waitui', 'Wakefield', 'Walcha', 'Walla Walla', 'Wallerawang', 'Walsh Bay',
'Warumbul', 'Watanobbi', 'Waterview', 'West Gosford', 'Westwood', 'Wherrol Flat', 'Whylandra Crossing',
'Winburndale', 'Windella', 'Wirlinga', 'Wongwibinda', 'Wymah', 'Yarragong', 'Yattalunga', 'Zetland'
);
// This JSON conversion method is a poor hack job, though it adequately demonstrates how things should work.
// Should use PHP 5.2 or later, for json functions
function arrayToJSON( $arr ) {
$jsonArray = array();
// bad and good characters, for both key and values
$badChars = array( "\n", "\r", "\"" );
$goodChars = array( "\\n", "\\r", "\\\"" );
// Check if this is a "numeric key" array (ie: a list).
// Note: this is only true, if they start at 0, otherwise they might be values
$isNumeric = true;
$firstValue = null;
foreach( $arr as $k => $v ) {
if( ! is_int($k) )$isNumeric = false;
if( $firstValue == null )$firstValue = $k;
}
if( $k != 0 )$isNumeric = false;
foreach( $arr as $k => $v ) {
// Note: addSlashed will quote ', which is NOT what we want.
$myKey = str_replace( $badChars, $goodChars, $k );
$myValue = str_replace( $badChars, $goodChars, $v );
if( is_array( $v ) )$jsonArray[$myKey] = arrayToJSON( $v );
else $jsonArray[$myKey] = '"' . $myValue . '"';
}
if( $isNumeric ) {
foreach( $jsonArray as $k => $v )$jsonArray[$k] = $v;
return "[" . join( ", ", $jsonArray ) . "]";
} else {
foreach( $jsonArray as $k => $v )$jsonArray[$k] = $k . ": " . $v;
return "{" . join( ", ", $jsonArray ) . "}";
}
}
$result = array();
$count = 0;
$limit = 20;
if( $searchMode == "partial" ) {
// This selects by matching within the comment. The MySQL statement is left for reference
$sql = sprintf("
SELECT * FROM suburb WHERE
suburb_name LIKE '%%%s%%'
ORDER BY suburb_name
LIMIT " . $limit, $query );
foreach( $suburb as $k => $v ) {
if( ( !( stripos($v,$query) === false )) && $count < $limit ) {
$result[$k] = $v;
$count += 1;
}
}
}
else if( $searchMode == "soundex" ) {
// This selects by matching with "sounds like". The MySQL statement is left for reference
$sql = sprintf( "
SELECT * FROM suburb WHERE
soundex(suburb_name) = soundex('%s')
ORDER BY suburb_name
LIMIT " . $limit, $query );
foreach( $suburb as $k => $v ) {
if( (soundex($v) == soundex($query)) && $count < $limit ) {
$result[$k] = $v;
$count += 1;
}
}
}
else {
// This selects by matching the start of the comment. The MySQL statement is left for reference
$sql = sprintf("
SELECT * FROM suburb WHERE
suburb_name LIKE '%s%%'
ORDER BY suburb_name
LIMIT " . $limit, $query ) ;
foreach( $suburb as $k => $v ) {
if( ( stripos($v,$query) === 0 ) && $count < $limit ) {
$result[$k] = $v;
$count += 1;
}
}
}
print arrayToJSON( $result );
?>