Outputting markers to Google Map

How do I get from region=441&region=446 in the url

to regions: [ 441, 446 ]

One issue I got is collecting the array values and ordering them correctly to pass over

The second part of my post deals with that - the $.get() function knows how to convert from a JS object to a query string (it uses $.param() under the hood).

I’m not sure what you mean.

sorry ye the second bit was me trying to explain myself again sorry.

I got your bit working now, so now Im trying to work out how to use a loop instead of the following simple if equal to below.

if ($row['Id_Rsrt']==$selectRegion) {
$newnode->setAttribute("type", utf8_encode('restaurant'));
} else {
$newnode->setAttribute("type", utf8_encode('hotel'));  
}

So I will have maybe just the single region value in some cases, but will have to deal with the comma separated multiple values to get the same different coloured markers

Could you share your PHP script again please? I saw that you posted it previously, but it looks like you’ve probably changed some things since then.

Ye its all a bit incomplete, but this is what I got so far, the php hasn’t been sorted much yet, but will post it and then you can see whats going on.

var data = {
	country: <?php echo $selectCountry ;?>,
	regions: [ <?php echo $regionData3 ;?> ]
};

$.get('phpsqlajax_genxml.php', data, function(response) {

That’s to send to ‘phpsqlajax_genxml.php’

if (!empty($_GET['selectCountry']) ? $_GET['selectCountry'] : null) {
$selectCountry = $_GET['selectCountry'];
}

if (!empty($_GET['selectRegion']) ? $_GET['selectRegion'] : null) {
$selectRegion = $_GET['selectRegion'];	
}

$query = mysql_query("SELECT Id_Rsrt, Nom_Rsrt, lat, lng, IdCntry_Rsrt, IdRsrt_Hot FROM tbl_resorts LEFT JOIN tbl_hotels ON (tbl_resorts.Id_Rsrt=tbl_hotels.IdRsrt_Hot) WHERE (tbl_resorts.IdCntry_Rsrt='".$selectCountry."') AND (lat !='') AND (lng !='') AND (tbl_hotels.Act_Hot='1')");

if ($row['Id_Rsrt']==$selectRegion) {
$newnode->setAttribute("type", utf8_encode('restaurant'));
} else {
$newnode->setAttribute("type", utf8_encode('hotel'));  
}

One thing to think about if both values are needed in this script, is that on some occasions, it might be that they don’t select a region and the map doesn’t show any markers

Where is $regionData3 coming from here?

I’m not entirely sure what you’re trying to do with these IF statements, but usually a ternary statement would be used (outside of an IF statement) to set a default value if the value you’re checking for isn’t set:

$selectCountry = isset($_GET['selectCountry']) ? $_GET['selectCountry'] : null;

If you’re using PHP7, you can use the null coalescing operator (??) to do the same thing with less code:

$selectCountry = $_GET['selectCountry'] ?? null;

I’d actually go a step further and use the filter_input function. This way we prevent non-integer values being passed to the script (more secure). Notice that the second time we’re specifying that selectRegion should always be an array, even if it contains only a single value:

$selectCountry = filter_input(INPUT_GET, 'selectCountry', FILTER_VALIDATE_INT);
$selectRegion = filter_input(INPUT_GET, 'selectRegion', FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY);

if (!$selectCountry) {
	$selectCountry = 1; // Set default value, or return error response
}

if (!$selectRegion) {
	$selectRegion = []; // Set as empty array (see code below)
}

// ...

// Check if the region code is in the array. We set $selectRegion to an empty array by default
// avoid this code throwing an error if no regions are chosen by the user.
if ( in_array($row['Id_Rsrt'], $selectRegion) ) {
	$newnode->setAttribute("type", utf8_encode('restaurant'));
} else {
	$newnode->setAttribute("type", utf8_encode('hotel'));  
}

Last but not least, is there a reason you’re returning the data as XML? Returning JSON would simply your code.

Morning Fretburner,

Don’t shout at me lol but this is how I got $regionData3

url - result.php?regions=436&regions=446&Country=58

if (!empty($_GET['regions']) ? $_GET['regions'] : null) {
 $regionsArray = array();
 foreach($_GET['regions'] as $regions) {	 
 $regionsArray[] = '\''.$regions.'\'';
 }
 
 $regionData = implode(',', $regionsArray);
 $regionData2 = implode(',', $regionsArray);
 $sqlregion =  ' AND IdRsrt_Hot IN ('. $regionData .' )';
 
 $regionData3 = str_replace("'", "", $regionData2); 
}

So if I was to echo $regionData3 out when two regions are selected you would see 436,446 so that’s how I have then echo’d them into the brackets below.

	  var data = {
	country: <?php echo $selectCountry ;?>,
	regions: [<?php echo $regionData3 ;?>]
  };

I’m also having trouble with this, its bringing back an error

if (!$selectRegion) {
$selectRegion = [];
}

and have gone with the code below just to let you know

$selectCountry = filter_input(INPUT_GET, 'selectCountry', FILTER_VALIDATE_INT);
$selectRegion = filter_input(INPUT_GET, 'selectRegion', FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY);

I’ve still got the problem where the map only shows when a region is selected, no map appears when no regions are selected, and at the moment its no map when no regions are selected, but map appears but without any markers when a region is selected, but I understand that’s why you did the if statement above that is causing an error.

I have then set this up ready

  if ( in_array($row['Id_Rsrt'], $selectRegion) ) {
 $newnode->setAttribute("type", utf8_encode('restaurant'));
  } else {
  $newnode->setAttribute("type", utf8_encode('hotel'));  
  }

Morning multichild,

You’re doing more work than you need to there. Here’s how I might do it:

$selectRegion = filter_input(INPUT_GET, 'selectRegion', FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY);
$selectRegion = $selectRegion ? $selectRegion : array(); // default to empty array if no regions supplied

if ($selectRegion) {
    $regionData = implode(',', $regionsArray);
    $sqlregion =  " AND IdRsrt_Hot IN ( $regionData )";
}

So now you can pass $selectRegion to your JS script using json_encode(), which will transform PHP data structures (such as arrays) into a format that JavaScript understands:

var data = {
  country: <?php echo $selectCountry; ?>,
  regions: <?php echo json_encode($selectRegion); ?>
};

Sounds like you’re using an old version of PHP (< 5.4) that doesn’t support the short array syntax. Change the middle line to

$selectRegion = array();

I made the change you suggested because of the 5.4 issue, and the error went away so thanks for that, but it didn’t make any difference to the map unfortunately.

The map doesn’t show unless a region is selected, and when a region is selected the map shows but no markers at all.

So because of the php version I used the two lines below

$selectCountry = isset($_GET['selectCountry']) ? $_GET['selectCountry'] : null;
$selectRegion = isset($_GET['selectRegion']) ? $_GET['selectRegion'] : null;

And it still has the same issue, so I’m guessing its something with the sending 2 values with the being an empty array, and then dealing with the array values to create the markers.

I’m stumped now sorry

I’ve just noticed one thing that probably isn’t helping. The keys in the object above (country and regions) need to be the same as the keys you’re using to access the data in the $_GET array in PHP. So try changing the above code to:

var data = {
  selectCountry: <?php echo $selectCountry; ?>,
  selectRegion: <?php echo json_encode($selectRegion); ?>
};

remember to look at the xml output to help see what is or isn’t happening. If the map isn’t showing when no region is selected it should just return an empty but valid xml page.

Ok I’m going to re-cap, as I think it will help me too with the updates.

I have the code below

$selectRegion = filter_input(INPUT_GET, 'regions', FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY);
$selectRegion = $selectRegion ? $selectRegion : array();
print_r(array_values($selectRegion));

Which when regions are selected prints out -

Array ( [0] => 432 [1] => 320 ) 

That feeds into below

var data = {
selectCountry: <?php echo $selectCountry;?>,
selectRegion: <?php echo json_encode($selectRegion);?>
};
$.get('phpsqlajax_genxml.php', data, function(response) {

to then be picked up on the next page

$selectCountry = isset($_GET['selectCountry']) ? $_GET['selectCountry'] : null;
$selectRegion = isset($_GET['selectRegion']) ? $_GET['selectRegion'] : null;

if (!$selectRegion) {
$selectRegion=array();
}
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

$query = mysql_query("SELECT Id_Rsrt, Nom_Rsrt, lat, lng, IdCntry_Rsrt, IdRsrt_Hot FROM tbl_resorts LEFT JOIN tbl_hotels ON (tbl_resorts.Id_Rsrt=tbl_hotels.IdRsrt_Hot) WHERE (tbl_resorts.IdCntry_Rsrt='".$selectCountry."') AND (lat !='') AND (lng !='') AND (tbl_hotels.Act_Hot='1')");

header("Content-type: text/xml");

while ($row = mysql_fetch_assoc($query)){
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("address", utf8_encode($row['Nom_Rsrt']));
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
if ( in_array($row['Id_Rsrt'], $selectRegion) ) {
$newnode->setAttribute("type", utf8_encode('restaurant'));
} else {
$newnode->setAttribute("type", utf8_encode('hotel'));  
}
$newnode->setAttribute("resortId", $row['Id_Rsrt']);
}
echo $dom->saveXML();
?>

And at this stage no map shows if no region is selected, and when a region is selected the map shows but no markers.

Noppy - I isolated phpsqlajax_genxml.php and was going to put a manual value to selectCountry and selectRegion but I couldn’t work out what to put as the value for selectRegion, as in is it in brackets as below

$selectRegion = [446,210];

which didn’t seem to work
I thin its nearly there, just out of my league now Im sorry guys.

Ok I isolated the country in the other page as below

$selectCountry = 56;

and the xml outputted as we needed it too

<?xml version="1.0"?>
-<markers>
<marker resortId="320" type="hotel" lng="-1.293059" lat="44.240669" address="Aquitaine"/>
<marker resortId="284" type="hotel" lng="4.853579" lat="44.048958" address="Provence Cote d Azur"/>
<marker resortId="371" type="hotel" lng="6.628764" lat="43.261864" address="Saint Tropez"/>
<marker resortId="408" type="hotel" lng="6.788878" lat="45.931976" address="Chamonix-Mont-Blanc"/>
<marker resortId="410" type="hotel" lng="6.974398" lat="43.657406" address="Opio"/>
<marker resortId="422" type="hotel" lng="7.928941" lat="42.175133" address="Corsica"/>
<marker resortId="431" type="hotel" lng="6.736246" lat="45.504730" address="Peisey Nancroix"/>
<marker resortId="433" type="hotel" lng="6.973114" lat="47.999008" address="Vosges Mountains"/>
<marker resortId="433" type="hotel" lng="6.973114" lat="47.999008" address="Vosges Mountains"/>
<marker resortId="435" type="hotel" lng="-1.168729" lat="45.686195" address="La Palmyre"/>
<marker resortId="422" type="hotel" lng="7.928941" lat="42.175133" address="Corsica"/>
<marker resortId="447" type="hotel" lng="6.573680" lat="45.298172" address="Val Thorens"/>
</markers>

So with that, think passing over country id only with an empty array isn’t working maybe, so I tried this, without any regions selected

	  var data = {
	selectCountry: 56,
	selectRegion: <?php echo json_encode($selectRegion);?>
  };

And no map appeared and this too and no map appeared

	  var data = {
	selectCountry: 56
  };

So that confuses it more I think

Do you have (or could you put) this version that you’re working on online somewhere? It might make things easier if we could see what requests the script is making to the server, and play around with submitting different params etc.

OK I worked something out which is good.

I isolated the next page and add the values as below

$selectCountry = 56;
$selectRegion = array(320,284);

And to my surprise I got exactly the output that its supposed to generate

<?xml version="1.0"?>
-<markers>
<marker resortId="320" type="restaurant" lng="-1.293059" lat="44.240669" address="Aquitaine"/>
<marker resortId="284" type="restaurant" lng="4.853579" lat="44.048958" address="Provence Cote d Azur"/>
<marker resortId="371" type="hotel" lng="6.628764" lat="43.261864" address="Saint Tropez"/>
<marker resortId="408" type="hotel" lng="6.788878" lat="45.931976" address="Chamonix-Mont-Blanc"/>
<marker resortId="410" type="hotel" lng="6.974398" lat="43.657406" address="Opio"/>
<marker resortId="422" type="hotel" lng="7.928941" lat="42.175133" address="Corsica"/>
<marker resortId="431" type="hotel" lng="6.736246" lat="45.504730" address="Peisey Nancroix"/>
<marker resortId="433" type="hotel" lng="6.973114" lat="47.999008" address="Vosges Mountains"/>
<marker resortId="433" type="hotel" lng="6.973114" lat="47.999008" address="Vosges Mountains"/>
<marker resortId="435" type="hotel" lng="-1.168729" lat="45.686195" address="La Palmyre"/>
<marker resortId="422" type="hotel" lng="7.928941" lat="42.175133" address="Corsica"/>
<marker resortId="447" type="hotel" lng="6.573680" lat="45.298172" address="Val Thorens"/>
</markers>

cool i was about to suggest you manually added it to the xml page to check if that part worked. So i am assuming it is a problem with passing that information from the map page to the xml page.

can you echo out the variables (data) on the map page so you can see what is being sent across?

document.write(data);

I think?? my javascript isn’t very good so someone might have to correct that.

There’s no need - you can use the browser’s dev tools to inspect the request that’s being made to the server for the markers (in fact, you can see what values are being output into the JS script just by viewing the page source).

I have actually re-started my computer as I was getting a few issues with the maps, maybe a cache issue, and as an update the map now doesn’t appear at all, so its not the case that the map appears when a region is selected, it doesn’t appear at all, so yes it seems its the passing over of the country value and the array.

I put this on the page and nothing outputted

	  var data = {
	selectCountry: <?php echo $selectCountry;?>,
	selectRegion: <?php echo json_encode($selectRegion);?>
  };
  
  document.write(data);

Ok I have uploaded the 2 pages I was working to the live server, the file name is result2.php instead of result, and it does connect up with the dev next page that goes with it

[Dev link](http://www.checksafetyfirst.com/result2.php?Country=58&regions=&category=&star=))

I know you can see the individual values of the variables but presumably you can’t see the data variable and what it holds can you? how can you see that bit? will be very useful to know myself as i am probably doing it the long way