PHP for-each循环不起作用?

时间:2016-01-27 15:58:32

标签: javascript php loops foreach maps

我的每个都不工作。不循环javascript代码。有人可以提供一些代码来帮忙吗?

		$guy= queryMysql("SELECT lat, long FROM members WHERE user='$guy'");
		while($data2 = mysql_fetch_array($guy)){

		 $latitude1= $data2['lat'];
		 $longitude1= $data2['long'];
		 
			echo "<script>function createMarker() {
			$.goMap.createMarker(
			{ 
				latitude: $latitude1, 
				longitude: $longitude1, 
				 animation: google.maps.Animation.DROP,
				title: 'Current users location', 
				html: { 
					content: '<p>This is your location $friend</p>', 
					popup: false 
				} 
			}
			);

			}</script>";

3 个答案:

答案 0 :(得分:1)

您需要在PHP循环中构建一个数组,并将该数据提供给您的javascript。这样的事情可以奏效:

PHP

<?php
$strOut = '';
if (sizeof($following)) {
    foreach ($following as $friend) {

        $friendsloc = queryMysql("SELECT homelocation, currentlocation FROM members WHERE user='$friend'");
        while ($data2 = mysql_fetch_array($friendsloc)) {

            $latitude1  = $data2['homelocation'];
            $longitude1 = $data2['currentlocation'];

            $strOut .= '{"lat": '.$latitude1.', "lon": '.$longitude1.'},';


        }
    }



}
$strOut = 'var locations = [' . rtrim($strOut,",") . ']';
?>

JavaScript的:

$(document).ready(function() {

    // get a Google map centred roughly on the John Dalton Building: 
    $('#map').goMap({
        latitude: 53.472342399999995,
        longitude: -2.2398096,
        zoom: 12,
        maptype: 'ROADMAP',
        scaleControl: true
    });



    <?php echo $strOut; ?>
    // now add a marker: 
    for(var i = 0; i < locations.length; i++) {
      $.goMap.createMarker({
          latitude: locations[i].lat,
          longitude: locations[i].lon,
          animation: google.maps.Animation.DROP,
          title: 'Current users location',
          html: {
              content: '<p>This is your location </p>',
              popup: false
          }
      });
    }




});

答案 1 :(得分:0)

您创建了许多createMarker()函数,最后一个覆盖了所有以前的函数。因此,当您稍后调用它时,您将使用最后一个参数进行一次调用。不要创建一个函数,将坐标推到某个存储区并在之后循环它。

答案 2 :(得分:0)

<?php

    $friendsloc = mysql_query("SELECT lat, long FROM members WHERE user='$friend'");
            while($data2 = mysql_fetch_array($friendsloc)){

             $latitude1= $data2['lat'];
             $longitude1= $data2['long'];

                echo "<script>function createMarker() {
                $.goMap.createMarker(
                { 
                    latitude: $latitude1, 
                    longitude: $longitude1, 
                     animation: google.maps.Animation.DROP,
                    title: 'Current users location', 
                    html: { 
                        content: '<p>This is your location $friend</p>', 
                        popup: false 
                    } 
                }
                );

                }</script>";
                    }
                        ?>