2016-03-16 54 views
0

我發現了一個非常聰明的代碼,用於在給出兩個參數時使用谷歌地圖計算駕駛時間和距離。我正在使用它來使用存儲在數據庫中的數據創建里程報告。php函數不會在循環中工作兩次

我的問題是我需要在同一個循環內兩次使用這個函數,但是當我這樣做時它會關閉循環並且根本不顯示任何信息。

請參考下面

function get_driving_information($start, $finish, $raw = false) 
{ 
if(strcmp($start, $finish) == 0) 
{ 
    $time = 0; 
    if($raw) 
    { 
     $time .= ' seconds'; 
    } 

    return array('distance' => 0, 'time' => $time); 
} 

$start = urlencode($start); 
$finish = urlencode($finish); 

$distance = 'unknown'; 
$time  = 'unknown'; 

$url = 'http://maps.googleapis.com/maps/api/directions/xml?origin='.$start.'&destination='.$finish.'&sensor=false'; 
if($data = file_get_contents($url)) 
{ 
    $xml = new SimpleXMLElement($data); 

    if(isset($xml->route->leg->duration->value) AND (int)$xml->route->leg->duration->value > 0) 
    { 
     if($raw) 
     { 
      $distance = (string)$xml->route->leg->distance->text; 
      $time  = (string)$xml->route->leg->duration->text; 
     } 
     else 
     { 
      $distance = (int)$xml->route->leg->distance->value/1000/1.609344; 
      $time  = (int)$xml->route->leg->duration->value/ 60; 
     } 
    } 
    else 
    { 
     throw new Exception('Could not find that route'); 
    } 

    return array('distance' => $distance, 'time' => $time); 
} 
else 
{ 
    throw new Exception('Could not resolve URL'); 
} 
} 

try 
{ 
$info = get_driving_information('fy1 4bj', 'ls1 5ns'); 
echo $info['distance'].' miles ' . 'That\'s about ' .$info['time'].' minutes drive from you'; 
} 
catch(Exception $e) 
{ 
echo 'Caught exception: '.$e->getMessage()."\n"; 
} 

此功能是,如果我註釋掉的第二個功能只會工作的代碼。

$sql="SELECT * FROM mileage"; 
$result=mysqli_query($con,$sql); 

?><table style="width:100%" border=1px><?php 
$i=0; 
while($row = mysqli_fetch_array($result)) 
{ 
$i=$i+1; 
     if (isset($row['Start'])){$start = $row['Start'];} 
     if (isset($row['Site'])){$finish2 = $row['Site'];} 
     if (isset($lastsite)) {$finish=$lastsite;} 
     if (isset($start) && isset($finish)){ 
$info = get_driving_information($start, $finish);} 
     if (isset($start) && isset($finish2)){ 
//$info2 = get_driving_information($start, $finish2); 
     } 
    if ($i>1){ 


     ?> 
     <td><?php echo $start; ?></td> <td><a target="_blank" href="https://www.google.co.uk/maps/dir/<?php echo $start . "/" . $lastsite ?>"> 
     Distance: <?php $drive=$info['distance']; 
     echo $drive; ?></td></tr> <?php  
    }?> 

     <tr><td> <?php echo $start ?></td><td><?php echo $finish2 ?> 
     </td><td><a target="_blank" href="https://www.google.co.uk/maps/dir/<?php echo $start . "/" . $finish2 ?>"> 
     Distance: 
     <?php 


     $drive2=$info2['distance']; 
     echo $drive2; 
     ?> </td></tr><tr><td> <?php echo $finish2 ?></td><?php 
     $lastsite=$finish2; 
}?> 
</table> 
+2

任何錯誤拋出?也許增加錯誤報告可能表明一些事情。 – apokryfos

+0

使用error_reporting(E_ALL)禁止沒有錯誤; –

+1

您可能會將空白字符串作爲目標傳遞給Google。如果$ row ['Site']是一個空白字符串,它仍然會將isset評估爲TRUE(因爲它未設置爲空)。我會嘗試檢查或回顯你的數據庫輸出來驗證。您可能想使用!empty()代替。否則,你可能會傳遞一些其他類型的無效輸入到谷歌API,在這種情況下調用代碼中的無效對象資源將導致致命錯誤。 – danielml01

回答

0

我沒有真的試圖修復你的代碼,但一般來說它應該工作。這裏是PHP代碼確實運行在一個循環的簡約例如:phpfiddle

function gdi($url){ 
$d=(new SimpleXMLElement(file_get_contents($url)))->route->leg; 
return array('from'=>$d->start_address,'to'=>$d->end_address, 
       'dist'=>$d->distance->text,'dur'=>$d->duration->text); 
} 
$url='http://maps.googleapis.com/maps/api/directions/xml?origin=fy1+4bj&destination='; 

foreach (array('ls1+5ns','Bünde','Goslar') as $dest){ 
$g=gdi($url.$dest);  
echo "The distance from $g[from] to $g[to] is $g[dist] and driving it takes about $g[dur].\n"; 
}