ammydon ammydon - 1 year ago 61
jQuery Question

While loop shows only one row?

include 'db.php';
$sql_locations = "SELECT * FROM location";
$result = mysqli_query($conn,$sql_locations);
$count = mysqli_num_rows($result);
$markers = array();

while($row = mysqli_fetch_array($result,MYSQL_ASSOC))

$markers = array(

} ?>

this is my code for fetching location from database. and i have this below script for showing map.

function initialize() {
var map;
var bounds = new google.maps.LatLngBounds();
var mapOptions = {
mapTypeId: "roadmap",
center: new google.maps.LatLng(20.5937, 78.9629), // somewhere in the uk BEWARE center is required
zoom: 1,
// Display a map on the page
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

// Multiple Markers
var markers = <?php echo json_encode( $markers );?>;

// Display multiple markers on a map
var infoWindow = new google.maps.InfoWindow();
var marker, i;

// Loop through our array of markers & place each one on the map
for (i = 0; i < markers.length; i++) {
var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0]

// Allow each marker to have an info window
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infoWindow.setContent(infoWindowContent[i][0]);, marker);
})(marker, i));

// Automatically center the map fitting all markers on the screen

//Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function (event) {

google.maps.event.addDomListener(window, 'load', initialize);

my map is working fine. im just facing problem to display my all records from database. my while loop only shows first from table please help me with this.
i want to display all locations from database with their markers.on print_r o $markers it shows only first row of table. on var_dump on $rows it shows all data. but im getting only one marker for my first row. i have total 2 rows in my table. thanks in advance.

Answer Source

You are overwriting your $markers variable in while loop. So you need to do like below:-

$markers[] = array('abc',$row['latitude'],$row['longitude']); 

Or (if the outer array is required)

$markers[] = array(array('abc',$row['latitude'],$row['longitude']));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download