问题:我有一个我调用的SQL查询。我希望能够将其放入一个数组中( tbh我现在这样做了),然后从每一行中拉出一个特定值,运行一些数学运算(我有这个),然后将这个数学运算的结果添加到查询的末尾(或开头)的值中,然后在相同的数组或新的数组中提供该值。
这是为了计算出构成SQL查询一部分的当前位置,然后计算出每个事件(每行一个)到该位置的距离。
SQL Query ->检查事件位置->运行距离当前位置上的数学到事件->重新编译为数组(SQL Query + Distance),准备json输出。
我要补充的是,我已经成功地在xml输出输出中做到了这一点,但当涉及到操作数组时,我仍然是一个新手……
谢谢
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;发布于 2012-04-27 05:25:36
想要使用SQL查询计算位置(纬度、经度)之间的距离吗?
如果是,则是这样的:https://developers.google.com/maps/articles/phpsqlsearch?hl=hu-HU
发布于 2012-04-27 05:41:06
这应该是可行的。
请注意,为了提高可读性,一些较长的代码已替换为[...]。
// 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;https://stackoverflow.com/questions/10341529
复制相似问题