繁体   English   中英

php功能在一个循环内将无法工作两次

[英]php function wont work twice within a loop

当找到两个参数时,我发现了一段非常聪明的代码,可以使用Google地图计算行驶时间和行驶距离。 我正在使用它来创建使用存储在数据库中的数据的里程报告。

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

请看下面的功能

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";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM