2016-04-23 66 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> 
相關問題