简体   繁体   English

PHP for-each循环无法正常工作?

[英]PHP for-each loop not working?

My for-each not working. 我的for-each不起作用。 Not looping javascript code. 不循环JavaScript代码。 Could someone please provide some code to help? 有人可以提供一些代码来帮助您吗?

  $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>"; 

You'd need to build an array in your PHP loop, and provide that data to your javascript. 您需要在PHP循环中构建一个数组,并将该数据提供给javascript。 Something like this could work: 这样的事情可能会起作用:

PHP 的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: 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
          }
      });
    }




});

You create numerous createMarker() functions and the last overrides all previous ones. 您创建了许多createMarker()函数,最后一个覆盖所有先前的函数。 So when you call it later, you get exactly one call with the last parameters. 因此,当您稍后调用它时,您只会得到一个带有最后一个参数的调用。 Do not create a function, push the coordinates to some storage and loop over it afterwards. 不要创建函数,将坐标推到某个存储中,然后在其上循环。

<?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>";
                    }
                        ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM