[英]Php Read SQL Query - Select 2 Values - Run Some Maths On Specific Row Value Add Value To New Column?
問題:我調用了一個SQL查詢。 我希望能夠將其拉入數組(tbh我現在要這樣做),然后從每一行中拉出特定值,運行一些數學運算(我有此運算法則),然后將此數學運算的結果添加到位於查詢的結尾(或開頭),然后可以在同一數組或新數組上使用它。
這是要計算出構成SQL查詢一部分的當前位置,然后要計算出每個事件的距離(每行一個)。
SQL查詢->檢查事件位置->在當前位置上運行事件距離數學->重新編譯為(SQL查詢+距離)數組以准備JSON輸出。
我將添加我已經設法針對xml輸出執行此操作,但是在處理數組時我仍然是菜鳥。
謝謝
人族
// 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;
是否要使用SQL查詢來計算位置(緯度經度)之間的距離?
如果是這樣,那就是這樣: https : //developers.google.com/maps/articles/phpsqlsearch?hl=hu-HU
這應該工作。
請注意,出於可讀性考慮,一些長代碼已替換為[...]
。
// 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.