我遇到了以下循環的問題,它會爲變量newLat[i]
生成空結果。但是,當我直接填充newLat[0]
(在最後2行)它工作正常。有什麼想法嗎?Javascript循環和數組
PHP:
$sql = "SELECT `id`, `name`, `lat`, `lng` FROM `markers` " ;
$result = $dbc->query($sql);
$hits = $result->num_rows ;
echo "<br /> Records = " ;
echo "$hits <br />";
while($row = $result->fetch_assoc()) {
$MarkerID[] = $row['id'];
$MarkerName[] = $row['name'];
$MarkerLat[] = $row['lat'];
$MarkerLng[] = $row['lng'];
}
和Javascript:
var myhits = <?php echo json_encode($hits); ?>;
var newLat = new Array (myhits);
for (var i = 0; i < myhits; i++) {
newLat[i] = <?php echo json_encode($MarkerLat[i]); ?>;
document.write (newLat[i]);
}
newLat[0] = <?php echo json_encode($MarkerLat[0]); ?>;
document.write (newLat[0]);