[英]Php Read SQL Query - Select 2 Values - Run Some Maths On Specific Row Value Add Value To New Column?
Problem: I have an SQL query which I call. 问题:我调用了一个SQL查询。 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.
我希望能够将其拉入数组(tbh我现在要这样做),然后从每一行中拉出特定值,运行一些数学运算(我有此运算法则),然后将此数学运算的结果添加到位于查询的结尾(或开头),然后可以在同一数组或新数组上使用它。
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查询一部分的当前位置,然后要计算出每个事件的距离(每行一个)。
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. SQL查询->检查事件位置->在当前位置上运行事件距离数学->重新编译为(SQL查询+距离)数组以准备JSON输出。
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.... 我将添加我已经设法针对xml输出执行此操作,但是在处理数组时我仍然是菜鸟。
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? 是否要使用SQL查询来计算位置(纬度经度)之间的距离?
If yes this is the way: https://developers.google.com/maps/articles/phpsqlsearch?hl=hu-HU 如果是这样,那就是这样: 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.