Search code examples
phparraysgoogle-maps-api-2markers

print markers with v2 API with different order than the table info


i have a sql query and i pass the result with mysql_fetch_array into a while loop, and i am printing a table. The think is that with the same info included in each row i am building the markers on the map. As i am building the map, the problem is that the map always centered at the final row of the table (C marker). Instead of this i want the map centered to the marker A (first row of the table). I was searching about to reverse the array but doesn't work.

  <?php
   while($info4 = mysql_fetch_array($result4))
   {    

   ?>   


      // A function to create the marker and set up the event window
      function createMarker(point, name, html, flag) 
      {
        //set the icon of the marker        
        var letteredIcon = new GIcon(baseIcon);
        letteredIcon.image = "markers/"+flag+".png";

         // Set up our GMarkerOptions object
        markerOptions = { icon:letteredIcon };
        var marker = new GMarker(point, markerOptions);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        // save the info we need to use later for the side_bar
        gmarkers.push(marker);
        // add a line to the side_bar html
        side_bar_html += '<td><a href="javascript:myclick(' + (gmarkers.length-1) + ')">' + name + '<\/a><\/td>';
         return marker;
      }


      // add the points   
      map.setCenter(new GLatLng(<?php print $info4['placeY'];?>,<?php print $info4['placeX'];?>), 14);    
      var point = new GLatLng(<?php print $info4['placeY'];?>,<?php print $info4['placeX'];?>);
      var marker = createMarker(point,"<?php print utf8_decode($info4['placeName']);?>","<?php print utf8_decode($info4['placeName'])."<br>".utf8_decode($info4['placeAddress'])."<br>".utf8_decode($info4['placeDistrict']);?>","<?=$flag;?>")
      map.addOverlay(marker);

    <?php  $flag=$flag+1;
    } ?> 

An example: Table:

A Sight-seeing Odeon

B Sight-seeing Phenomenon of Tides

C Sight-seeing Red House

In this example the map is centered on the C marker instead of A marker which i want to see.


Solution

  • So, instead of using while loop, the correct code is below.

        //make an array
        $rows = array();
    while ($row=mysql_fetch_array($result4)){
    //push the rows to the empty array
        array_push($rows, $row);
    }
    // reverse the order of the rows
    $reversedarray=array_reverse($rows);
        //use the info of each row as the variable $info4 
    foreach ($reversedarray as $info4) {
        //the code