2014-01-29 68 views
0

我正在做这个大学作业。我正在做一个基于易趣和谷歌地图的API的Web应用程序。在实践中,我使用易趣进行了api调用,并将结果放在一张表中。现在,点击我得到的每个项目的位置,我想要在谷歌地图中对地址进行地理编码。我写了易趣的API谷歌地图API的PHP代码和JavaScript代码如下:php-javascript:无法更新变量

为eBay API的PHP代码...

foreach($resp->searchResult->item as $item){ 
      $_i = 1; 

      // Determine which price to display 
      if ($item->sellingStatus->currentPrice) { 
       $price = $item->sellingStatus->currentPrice; 
      } else { 
       $price = $item->listingInfo->buyItNowPrice; 
      } 

      $itemLocation = $item->location; 
      // For each item, create a cell 
      $results .= "<tr> \n<td $thisCellColor valign=\"top\"> \n"; 
      $results .= "<img src=\"$item->galleryURL\"></td> \n"; 
      $results .= "<td $thisCellColor valign=\"top\"> <p><a href=\"" . $item->viewItemURL . "\">" . $item->title . "</a></p>\n"; 
      $results .= 'Shipped to: <b>' . $item->shippingInfo->shipToLocations . "</b><br> \n"; 
      $results .= 'Current price: <b>$' . $price . "</b><br> \n"; 
      $results .= "Location: <INPUT TYPE=\"button\" id=\"$_i\" VALUE=\"$itemLocation\" onclick='clickToGeocode($_i);' <br>"; 
      $results .= "</FORM> \n"; 
      $results .= "</td> </tr> \n\n"; 
      $_i = $_i + 1; 
     } 

JavaScript代码谷歌地图:

function clickToGeocode(id){ 
    var address = document.getElementById(id).value; 
    document.write(id); 
    geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     var marker = new google.maps.Marker({ 
     map: map, 
     position: results[0].geometry.location 
     }); 
    } else { 
     alert('Geocode was not successful for the following reason: ' + status); 
    } 
    }); 
} 

问题是,我每次尝试对地理位置进行地理编码时,地图都会转到在ebay中找到的第一个项目的位置..我的意思是$ _i(在php中)和id(在javascript中)始终是1 ..我该如何解决这个问题?任何想法?谢谢..

回答

1

您正在重置$_i1每次。初始值需要设置之前的循环,不它:

$_i = 1; 

foreach($resp->searchResult->item as $item) 
{ 
+0

@MichaelRushton ...我做了一个愚蠢的问题......我完全错过了......谢谢:) – bece

0

你的HTML被打破:

$results .= "Location: <input [..snip...] onclick='clickToGeocode($_i);' <br>"; 
                     ^---- here 

你永远不会关闭<input>标签,例如您在指定位置缺少>