I am trying to get the output of the lat and lon coordinates from ios (this is working fine), send it to php to query with MySQL and have the php send an xml document back to ios(this step is not working because it is not bringing back the mysql entry within that location), then parsing it on iOS UItableview (this is working fine too). I am trying to get this to work with XML cause I've gotten a simpler xml script running already on it. But probably due to mistakes from inexperience in php, I cannot get this php script working! What am I doing wrong in my php script? Thanks! Oh, and also, the data categories in mysql consist of "lon" and "lat" and "name" (for the name of a nearby friend or family)! AND in case anyone was wondering, this is an evolved version of an earlier script (that was also producing the same results): php query for iOS latitude and longitude not searching for nearby mysql lat and lon with a xml output
<?php
define( 'LATMILES', 1 / 69 );
define( 'LONMILES', 1 / 53 );
if ( isset( $_GET['lat'] ) ) { $lat = (float)$_GET['lat']; } //Recieve ios input from: NSString *urlString = [NSString stringWithFormat:@"http://www.mysite.com/loc.php?lat=%g&lon=%g&radius=100&q=%@", latitude, longitude, searchBar.text?searchBar.text:@""];
if ( isset( $_GET['lon'] ) ) { $lon = (float)$_GET['lon']; } //Recieve ios input from: NSString *urlString = [NSString stringWithFormat:@"http://www.mysite.com/loc.php?lat=%g&lon=%g&radius=100&q=%@", latitude, longitude, searchBar.text?searchBar.text:@""];
if ( isset( $_GET['radius'] ) ) { $radius = (float)$_GET['radius']; } //Recieve ios input from: NSString *urlString = [NSString stringWithFormat:@"http://www.mysite.com/loc.php?lat=%g&lon=%g&radius=100&q=%@", latitude, longitude, searchBar.text?searchBar.text:@""];
$minlat = $lat - ( $radius * LATMILES );
$minlon = $lon - ( $radius * LONMILES );
$maxlat = $lat + ( $radius * LATMILES );
$maxlon = $lon + ( $radius * LONMILES );
$dbh = new PDO('(censored private information');
$sql = 'SELECT lat, lon, name FROM locations WHERE lat >= ? AND lat <= ? AND lon >= ? AND lon <= ?';
$params = array( $minlat, $maxlat, $minlon, $maxlon );
if ( isset( $_GET['q'] ) ) {
$sql .= " AND name LIKE ?";
$params []= '%'.$_GET['q'].'%';
}
$q = $dbh->prepare( $sql );
$q->execute( $params );
$doc = new DOMDocument();
$r = $doc->createElement( "locations" );
$doc->appendChild( $r );
foreach ( $q->fetchAll() as $row) {
$dlat = ( (float)$row['lat'] - $lat ) / LATMILES;
$dlon = ( (float)$row['lon'] - $lon ) / LONMILES;
$d = sqrt( ( $dlat * $dlat ) + ( $dlon * $dlon ) );
if ( $d <= $radius ) {
$e = $doc->createElement( "location" );
$e->setAttribute( 'lat', $row['lat'] );
$e->setAttribute( 'lon', $row['lon'] );
$e->setAttribute( 'name', $row['name'] );
$r->appendChild( $e );
}
}
print $doc->saveXML();
?>