i dont find to my error plese help me


this code i using to loading to php array

while($newarray = mysql_fetch_array($query))
	{
	
		
		$latt[$count] = $newarray['lat'];
		$lonn[$count] = $newarray['lon'];
		$userr[$count] = $newarray['user'];
	
			$count++;
		

	}

this part my javascript code

var cout= <?php echo $count ?>;
        var lat = new Array();
        var lon = new Array();
	var user = new Array();
				
	<?php $count =0; ?>
	
        var i;
	
       for ( 	var ii=0 ; ii<cout ; ii++)
	        {
		    <?php $count++ ?>
					
		     user[ii] = ("<?php echo $userr[$count]?>");
		     lat[ii] = ("<?php echo $latt[$count]?>");
		     lon[ii] = ("<?php echo $lonn[$count]?>");
						
		     var point = new GLatLng(lat[ii],lon[ii]);
		     map.setCenter(point, 10);
		     var marker = new GMarker(point);
		     map.addOverlay(marker);
		     GEvent.addListener(marker, "click", function()
		     {marker.openInfoWindowHtml(user);});
			}

error is given by just fist point. I want to do to get the coordinates of all databases. javascrpit array not working.

Dani AI

Generated

Good call by — generating the JavaScript data on the server and outputting it as arrays is the right pattern. The original problem came from trying to increment a PHP counter inside a client-side loop and echoing element-by-element: that easily produces off-by-one indexing and repeated or missing values. Build one PHP data structure and emit it to JS in one step instead of interleaving server-side increments and client-side iteration.

A simple, robust pattern is to collect rows in PHP and emit a single JSON value for the browser to consume, then loop in JavaScript. For example, create a PHP array of rows and echo it once with json_encode, then iterate points in JS. This avoids quoting/escaping mistakes and keeps numeric lat/lon values numeric on the client. (php.net)

Also watch the marker click handler: a listener that references loop variables can capture the same (shared) variable for every marker, so every click shows the wrong info. Two fixes: (1) wrap the listener registration in an IIFE to bind the current marker and label, or (2) in modern code declare the loop index with let so each iteration has its own binding. Either approach ensures each marker’s click callback uses the correct user text. (developer.mozilla.org)

One more note for readers years later: the thread’s code uses the Maps JavaScript API v2, which has been deprecated and is no longer available — migrate to the current Maps JavaScript API (v3+) or a maintained library (for example Leaflet) when updating this page. That future-proofs the map and avoids runtime errors caused by removed API methods. (developers.google.com)

This complements ’ solution and prevents the original indexing and event-binding pitfalls that caused only the first point to appear correctly.

Recommended Answers

All 5 Replies

You cannot mix javascript and php like that. You need to create a php file that generates the complete javascript array definition.

this all my code

<?php

	$mysql_conn = mysql_connect("localhost","root","");
	if ( ! $mysql_conn ) die ("MySQL Connection is impossible!");

	mysql_select_db("gps_mapping" , $mysql_conn) 
		or die ("Database connection is impossible!" . mysql_error() );
		

	
	$query = mysql_query("SELECT user,lat,lon FROM current_coor");
		
	$count=0;
	
	while($newarray = mysql_fetch_array($query))
	{
	
		
		$latt[$count] = $newarray['lat'];
		$lonn[$count] = $newarray['lon'];
		$userr[$count] = $newarray['user'];
	
			$count++;
		

	}

	

	mysql_close($mysql_conn)
	

		?>
		
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>GPS Traffic Control System</title>
		<script src="" type="text/javascript"></script>
		<script type="text/javascript">

					
					
					
			
	
		var map;
		var route;
		var lastpoint;
		var header = '&lt;waypoints&gt;';
		var data = "";
		var footer = '&lt;/waypoints&gt;';

		function load() {
			if (GBrowserIsCompatible()) {   
				route = document.getElementById("route");
				map = new GMap2(document.getElementById("map"));
					
			var cout= <?php echo $count ?>;
			var lat = new Array();
			var lon = new Array();
			var user = new Array();
				
			<?php $count =0; ?>
			   var i;
			   for ( var ii=0 ; ii<cout ; ii++)
				{
				<?php $count++ ?>
					
		               user[ii] = ("<?php echo $userr[$count]?>");
		        	lat[ii] = ("<?php echo $latt[$count]?>");
				lon[ii] = ("<?php echo $lonn[$count]?>");
						
				var point = new GLatLng(lat[ii],lon[ii]);
				map.setCenter(point, 10);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				GEvent.addListener(marker, "click", function()
				{marker.openInfoWindowHtml(user);});
					
				
						
					}	
					
					}

			}

	</script>
	</head>

	<body bgcolor="#000000" onload="load()" onunload="GUnload()">
		<center>
			<table>
				<tr>
					<td><div id="map" style="width: 800px; height: 500px;"></div></td>
				</tr>
			</table>
		</center>
	</body>
</html>
<?php
  $mysql_conn = mysql_connect("localhost","root","");
  if ( ! $mysql_conn )
    die ("MySQL Connection is impossible!");
  
  mysql_select_db("gps_mapping" , $mysql_conn) or die ("Database connection is impossible!" . mysql_error() );
  $query = mysql_query("SELECT user,lat,lon FROM current_coor");
  while($newarray = mysql_fetch_array($query)) {
    $latt[] = $newarray['lat'];
    $lonn[] = $newarray['lon'];
    $userr[] = $newarray['user'];
  }
  mysql_close($mysql_conn);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>GPS Traffic Control System</title>
  <script src="" type="text/javascript"></script>
  <script type="text/javascript">
    var map;
    var route;
    var lastpoint;
    var header = '&lt;waypoints&gt;';
    var data = "";
    var footer = '&lt;/waypoints&gt;';
    
    var cout = <?php echo count($latt); ?>;
    var lat = new Array(<?php echo implode(',', $latt); ?>);
    var lon = new Array(<?php echo implode(',', $lonn); ?>);
    var user = new Array(<?php echo "'" . implode("','", $user) . "'"; ?>);
    
    function load() {
      if (GBrowserIsCompatible()) {
        route = document.getElementById("route");
        map = new GMap2(document.getElementById("map"));
        
        for ( var ii=0 ; ii < cout ; ii++) {
          var point = new GLatLng(lat[ii],lon[ii]);
          map.setCenter(point, 10);
          var marker = new GMarker(point);
          map.addOverlay(marker);
          GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(user[ii]); });		
        }	
      }
    }
  </script>
</head>
<body bgcolor="#000000" onload="load()" onunload="GUnload()">
  <center>
    <table>
      <tr>
        <td><div id="map" style="width: 800px; height: 500px;"></div></td>
      </tr>
    </table>
  </center>
</body>
</html>

thank you so much code is working

No problem. Please mark your thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.