Problem: I have an SQL query which I call. I want to be able to pull this in to an array ( tbh I do this now ) but then pull a specific value from each row, run some maths ( I have this ) and then add the result of this maths in to a value at the end ( or beginning ) of the query which is then made available either on the same array or a new one.
This is to work out a current location which forms part of the SQL query but then to work out the distance for each event ( one per row ) from that.
SQL Query -> check event location -> run distance maths on current location to event -> recompile to array as (SQL Query + Distance) ready for json out put.
I will add I've managed to do this for xml out put but I'm still a noob when it comes to manipulating arrays....
Thanks
Terran
// Connect to database server
$con = mysql_connect($config_databaseServer,$config_databaseUsername,$config_databasePassword) or die(mysql_error());
mysql_select_db($config_databaseName, $con);
// Access tables
$sql = "SELECT * FROM " . $config . " WHERE LAT <" . $toplat . " AND LAT > " . $bottomlat . " AND LONG > " . $leftlon . " AND LONG < " . $rightlon . " AND VALIDPERIOD_STARTOFPERIOD < '" . $nowtime . "' AND VALIDPERIOD_ENDOFPERIOD > '" . $nowtime ."')";
// Execute query
$result = mysql_query($sql) or die(mysql_error());
$lata = $latitude;
$lona = $longitude;
// Need to populate these from each row values in the query
$latb = $sqllat;
$lonb = $sqllong;
// need to place this in to a colum at the end of each row
$distancefromevent = coordDistance($lata, $longa, $latb, $lonb, "m");
// need to reform all the above so I can carry on and use the code that follows
$responses = array();
if(mysql_num_rows($result)) {
while($response = mysql_fetch_assoc($result)) {
$responses[] = array('dataitem'=>array_map('utf8_encode',$response));
}
}
header('Content-type: application/json');
$json = json_encode(array($config_datexdeftype=>$responses));
$callback = $_GET[callback];
echo $callback . '(' . $json . ')';
};
return TRUE;
Want you to calculate the distance between a location (latitude longitude) with an SQL query?
If yes this is the way: https://developers.google.com/maps/articles/phpsqlsearch?hl=hu-HU
This should work.
Note that some long code has been replaced with [...]
for the readability.
// Connect to database server
$con = mysql_connect([...]) or die(mysql_error());
mysql_select_db($config_databaseName, $con);
// Access tables
$sql = "SELECT * FROM " . $config . " WHERE [...]";
// Execute query
$result = mysql_query($sql) or die(mysql_error());
// Prepare the loop
$lata = $latitude;
$lona = $longitude;
$responses = array();
// Loop over each records (stay correct even if there is no record)
while($response = mysql_fetch_assoc($result)) {
// we assume that latitude and longitude are given by
// columns 'lat' and 'lon' in the SQL query.
$latb = $response['lat'];
$lonb = $response['lon'];
$distancefromevent = coordDistance($lata, $longa, $latb, $lonb, "m");
$response['distance'] = $distancefromevent;
$responses[] = array('dataitem'=>array_map('utf8_encode',$response));
}
header('Content-type: application/json');
$json = json_encode(array($config_datexdeftype=>$responses));
$callback = $_GET[callback];
echo $callback . '(' . $json . ')';
return TRUE;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.