2016-04-23 37 views
-1

我有一个SQL数据库与GPS点,我想将它们设置为我的谷歌地图API标记。 在初始化sql连接之后,我开始了一个while循环,其中我首先实现了一个php脚本,然后是javascript。但是由于某种原因它不起作用。我不知道我犯了什么错误? 的代码如下所示:我怎样才能让PHP的PHP回声变量在PHP循环工作?

......stuff before 
    <?php 
    $i=1; 
    while($i <48): 
    ?> 
<!-- SELECT GPS DATA FROM SQL DATABASE AND CONVERT INTO DECIMALS -------------------> 
    <?php 
    //select 4 numbers (longitude, altitude) from database 

    $result = mysql_query("SELECT kglat, kmlat, kglon, kmlon FROM falpeg WHERE indix='$i'"); 
    .... 
    .... 
    .... (converting into decimals for google maps, used mysql_fetch_row, this part should be alright) 
    ... 
    $klat = ($kmlat/60) + $kglat; 
    round ($kord, 5); 
    $klon = ($kmlon/60) + $kglon; 
    ?> 

<!-- START JAVASCRIPT ---------------------------------------> 
    <script type="text/javascript"> 
    var "<?php echo $i; ?>" = {lat: "<?php echo $klat; ?>", lng: "<?php echo $klon; ?>"; 
    var marker=new google.maps.Marker({ 
    position: "<?php echo $i; ?>", 
    icon:'cross.png' 
    }); 

    marker.setMap(map); 
    var infowindow = new google.maps.InfoWindow({ 
    content:"Hello World!" 
    }); 

    marker.addListener('click', function() { 
    infowindow.open(map, marker); 
    }); 
    </script> 

<!-- END WHILE LOOP, SET I++ -------------------> 
    <?php 
    $i++; 
    endwhile; 
    ?> 
+0

'变种 “<?PHP的echo $ I;>”'?这是什么?你打开你生成的HTML吗?开发者控制台? –

+0

我可以看到它的错误,但我怎样才能让我的VAR名称? –

+0

用于存储相同类型的变量使用数组。 –

回答

0

让我们看看,如果我们可以清理这个了一下。

•使用PDO(很多好处,但我们只会说它更简单)
•构建一个查询,获取所需的所有数据,而不是在循环中运行单个查询。
•尽量不要在各处回应PHP ..!
•当您说echo $ i时,我会看到您要做什么,但是,$ i只是一个整数值,所以您需要先将数据存储到数组,然后使用$ myArray [$ i] ;
•利用json_encode获得在需要的格式

PHP的信息:

<?php 

// connect to the DB using PDO 
$db = new PDO('mysql:host=host;dbname=dbname','username','password'); 

// Create a SQL statement that will return ALL of the rows you need 
$min = 0; 
$max = 47; 
$sql = "SELECT kglat, kmlat, kglon, kmlon FROM falpeg WHERE indix BETWEEN $min AND $max;"); 

$query = $db->query($sql); // run the query 
$results = $query->fetchall(); // fetch the results 

// these results are in an array..! You can access each item 
// $results[index]['columnname'] index is just an integer starting a 0 
//  ex. $results[0]['kglat'] is the kglat value of the first element 

$markers = Array(); // we'll use this to store the marker info in the same structure 
// you're using in the google.maps.create 

// Now, let's loop through each item in the results and populate our $markers array 
foreach($results as $i=>$m){ 

    // $m is just a single row from $result [a single (m)arker if you will..] 
    // so $m['kmlat'] is how you access the 'kmlat' property 
    // of the current row as we loop through. $i is the row number. 

    // structuring this so it matches the format you 
    // need when you pass the info to google maps 
    $markers[$i]['position']['lat'] = ($m['kmlat']/60) + $m['kglat']; // add 'lat' value to $markers 
    $markers[$i]['position']['lng'] = ($m['kmlon']/60) + $m['kglon']; // add 'lng' value to $markers 
    $markers[$i]['icon'] = 'cross.png';  
} 

// this will format the data so javascript can use it easily 
// you don't need to manualy do it. 
$markers = json_encode($markers); 

?> 

的Javascript:

<script> 
    var markerData = <?php echo $markers ?>; //just plop in our structure data, shouldn't need to format (hopefully) 

    markerData.forEach(function($i){   // we shouldn't need to format $i because json_encode did that for us 
     markers[] = new google.maps.Marker($i); // 'markers' will be an array of goole map markers 
    }); 

</script> 
相关问题