我没有太多的编程经验。
我有一些我正在适应的代码,我不明白变量是如何从PHP传递到JavaScript的。我怀疑我错过了一些基本理论,但迄今为止我一直无法在网络上追踪它。谷歌地图标记信息被传递如下: -
使用Javascript的PHP变量
<?php
$mrk_cnt = 0;
while ($obj = $res->fetch_object()){
$username[$mrk_cnt] = $obj->username;
$inf[$mrk_cnt] = $obj->title;
$lat[$mrk_cnt] = $obj->lat;
$lng[$mrk_cnt] = $obj->long;
$begin[$mrk_cnt] = $obj->begin;
$end[$mrk_cnt] = $obj->end;
$distance[$mrk_cnt] = $obj->distance;
$marker_type[$mrk_cnt] = $obj->type;
$icon[$mrk_cnt]=(($marker_type[$mrk_cnt]=='inquirer')? 'red_marker': 'blue_marker');
$mrk_cnt++;
}
?>
而且上有一些JavaScript的PHP的呼应: -
<script>
<?php
for ($lcnt = 0; $lcnt < $mrk_cnt; $lcnt++){
echo "var point$lcnt = new google.maps.LatLng($lat[$lcnt], $lng[$lcnt]);\n";
echo "var mrktx$lcnt = \"$inf[$lcnt]\";\n";
echo "var infowindow$lcnt = new google.maps.InfoWindow({ content: mrktx$lcnt });";
echo "var marker$lcnt = new google.maps.Marker({position: point$lcnt, map: map, icon:" . $icon[$lcnt] . "});\n";
echo "google.maps.event.addListener(marker$lcnt,'click', function() {infowindow$lcnt.open(map,marker$lcnt);});\n";
echo "bounds.extend(point$lcnt);\n";
}
?>
map.fitBounds(bounds);
}
</script>
如何是PHP变量 - $纬度[],例如 - 找到他们的方式进入JavaScript?它们是用PHP声明的,它们不被连接或引用。假设JavaScript会知道$ lat [$ lcnt]将会是在$ php中声明的$ lat [$ mkr_cnt],$ lcnt设置为= $ mkr_cnt。
非常感谢。
谢谢,这是我缺少的信息的重要一点。我发现这个页面对我来说很清楚:http://hydrogen.informatik.tu-cottbus.de/wiki/index.php/PHP_Variable_Interpolation – user2790911