PHP 函数在一个循环中不会工作两次


php function wont work twice within a loop

我发现了一个非常聪明的代码,用于在给出两个参数时使用谷歌地图计算行驶时间和距离。 我正在使用它使用存储在数据库中的数据创建里程报告。

我的问题是我需要在同一个循环中使用此功能两次,但是当我这样做时,它会关闭循环并且根本不显示任何信息。

请参阅下面的功能

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>

我并没有真正尝试修复您的代码,但一般来说它应该可以工作。下面是一个在循环中运行的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";
}