Javascript 在MVCarray中指定纬度和经度
我正在尝试获取我的mysql数据,我在中读了一些文章Javascript 在MVCarray中指定纬度和经度,javascript,php,mysql,arrays,database,Javascript,Php,Mysql,Arrays,Database,我正在尝试获取我的mysql数据,我在中读了一些文章 而不是var taxiData=[new google.maps.LatLng(37.782551,-122.445368)] 现在我正在尝试获取这些纬度和经度的值 您试图在PHP中执行js示例。实际上,您需要了解一些web开发方面的知识,但您可以尝试以下方法,以使其立即工作: <?php /* You should add all needed external js scripts above */?> <scr
而不是var taxiData=[new google.maps.LatLng(37.782551,-122.445368)]代码>
现在我正在尝试获取这些纬度和经度的值 您试图在PHP中执行js示例。实际上,您需要了解一些web开发方面的知识,但您可以尝试以下方法,以使其立即工作:
<?php /* You should add all needed external js scripts above */?>
<script type="text/javascript">
//Here You should put everything before the points array declaring from the example
<?php
$locations = mysql_query("select * from locations");
echo 'var taxiData = [';
while ($locat = mysql_fetch_array($locations))
echo "new google.maps.LatLng({$locat['latitude']}, {$locat['longitude']}),\n";
echo '];';
//Here You should put everything after the points array declaring from the example
?>
</script>
//在这里,您应该将所有内容放在示例中声明的点数组之前
您似乎在尽情地混合php
和javascript。如果要发出javascript命令,必须确保它在浏览器中看起来像javascript。也许像
<?php
$locations = mysql_query("select * from locations");
?>
<?php
$lats = ""; // string with latitude values
$lngs = "";
while ($locat = mysql_fetch_array($locations))
{
$lats = $locat['latitude'];
$lngs = $locat['longitude'];
$taxiString = "new google.maps.LatLng($lats,$lngs)";
?>
<script type="text/javascript">
var taxiData = [<?=$taxiString?>];
// do whatever you want in javascript
</script>
}
?>
稍微分解一下(为了清晰起见,添加一些回车):
-这是我原始文件中的文本
它可以工作0次这是php echo命令的结果
-原始文件中的文本
警报(“-literal text
它工作了0次!-这是
"); - 文字
-文字
然后是一些疯狂的事情——同样的事情会重复(因为php循环)——但使用不同的值(1而不是0):
它可以工作1次!
警报(“它工作1次!”);
-最后一行没有重复,因为它在php循环之外
最后评论:
<?= $myString; ?>
是的缩写
<?php echo $myString; ?>
您的问题是什么?发生了什么,怎么会不像你期望的那样?$taxiData=[new google.maps.LatLng($lats,$lngs)];是的,谢谢你指出这一点:(我现在已经开始工作了。
<html>
It works 0 times!<script type="text/javascript">
alert("It works 0 times!");
</script>
It works 1 times!<script type="text/javascript">
alert("It works 1 times!");
</script>
</html>
<html> - this was text in my original file
It works 0 times! - this was the result of a php echo command
<script type="text/javascript"> - literal text in original file
alert(" - literal text
It works 0 times! - the result of <?= $myString; ?>
"); - literal text
</script> - literal text
It works 1 times!<script type="text/javascript">
alert("It works 1 times!");
</script>
</html> - this last line did not repeat because it's outside the php loop
<?= $myString; ?>
<?php echo $myString; ?>