Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm doing this for an university assignment. i'm doing a web application based on the apis of ebay and google maps. in practice, i make an api call in ebay and i put the results in a table. Now, clicking on the location of each item i got as a result, i want to geocode the address in a google map. i wrote the php code for ebay apis and the javascript code for the google maps apis as follows:

php code for ebay apis...

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 code for google maps:

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

The problem is that every time I try to geocode a location the map goes to the location of the first item found in ebay..I mean the $_i (in php) and the id (in javascript) is always 1.. How can I resolve this??any idea?? thanks..

share|improve this question
add comment

2 Answers

You're resetting $_i to 1 each time. The initial value needs to be set before the loop, not within it:

$_i = 1;

foreach($resp->searchResult->item as $item)
{
share|improve this answer
 
@MichaelRushton...what a stupid question I have done..I completly missed this...thanks :) –  bece Jan 29 at 17:01
add comment

Your html is broken:

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

You never close the <input> tag, e.g. you're missing a > at the indicated spot.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.