0

问题:我有一个我调用的 SQL 查询。我希望能够将其拉入一个数组(我现在这样做),然后从每一行中提取一个特定值,运行一些数学运算(我有这个),然后将这个数学运算的结果添加到一个值中查询的结尾(或开头),然后在同一数组或新数组上可用。

这是为了计算出构成 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;
4

2 回答 2

0

这应该有效。

[...]请注意,为了便于阅读,一些长代码已被替换为。

// 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;
于 2012-04-26T21:41:05.990 回答
0

想要通过 SQL 查询计算某个位置(经纬度)之间的距离吗?

如果是,这就是方式: https ://developers.google.com/maps/articles/phpsqlsearch?hl=hu-HU

于 2012-04-26T21:25:36.357 回答