I have a PHP script that returns a latitude and longitude from the database.
Then I have a Javascript loop that loops according to the number of rows the PHP query returns and populate my data object with the logitude and latitude at a given index. The challenge now is - how do I get the index without hard coding it?
PHP
$DBhost = 'localhost';
$DBuser = 'root';
$DBpass = '';
$DBname = 'symptoms';
$con = mysqli_connect($DBhost, $DBuser, $DBpass) or die(mysql_error());
mysqli_select_db($con, $DBname);
$query = mysqli_query($con, 'SELECT latitude,longitude FROM markers') or die(mysql_error());
$longArr = array();
$latArr = array();
$count = 0;
while ($row = mysqli_fetch_array($query)) {
$lat = $row['latitude'];
$lon = $row['longitude'];
$latArr[$count] = $lat;
$longArr[$count] = $lon;
++$count;
}
JavaScript
var latArr = [];
var longArr = [];
var markersD = [];
var num = <?php echo $count ?>;
for (var i = 0; i < num; i++) {
var data = {
lat: '<?php echo $latArr[1]?>',
lon: '<?php echo $longArr[1]?>'
}
console.log(data);
}