我正在学习php并做一个小项目。我在数据库中有一个city_name表,其中包含列id,名称,经度和纬度。它最多可能有10-15行。现在,当用户加载index.php时,我执行此查询以从city_name表中获取所有数据
$city_name = mysql_query('SELECT * FROM city_name');
从上面的查询中找到的值,我想以下列格式创建一个javascript变量。我想在标签内的相同index.php中执行此操作。
var locations = [
['IMaxx', 50.780941, 6.079134],
['Aachen-Elisenbrunnen', 50.771680, 6.086472],
['Uni-Cinema', 50.776185, 6.079306],
['Alter-Posthof', 50.771705, 6.086561],
['Cinemaxx', 50.770754, 6.084942]
];
我该怎么做?
答案 0 :(得分:0)
只需将其作为JSON字符串回显:
<?php
$arrayFromDB = [
['IMaxx', 50.780941, 6.079134],
['Aachen-Elisenbrunnen', 50.771680, 6.086472],
['Uni-Cinema', 50.776185, 6.079306],
['Alter-Posthof', 50.771705, 6.086561],
['Cinemaxx', 50.770754, 6.084942]
];
echo "<script>var locations = ".json_encode($arrayFromDB).";</script>";
?>