【发布时间】:2021-09-20 07:19:57
【问题描述】:
我尝试制作一个简单的搜索功能来查找医院名称,然后它会显示在地图上,我在搜索时使用了 $_POST 超全局变量,它会在数据库中查找结果。该代码稍微有效,并且稍微有效,它只在地图上显示一个标记,即使它应该根据您在文本框中输入的文本显示多个结果。
<?php
if(isset($_POST['search'])){
$term = $_POST['term'];
$query = "SELECT * from hospitals WHERE hname LIKE '%$term%' ";
$search_query = mysqli_query($connection, $query);
if(!$search_query){
die("QUERY FAILED" . mysqli_error($connection));
}
$count = mysqli_num_rows($search_query);
if($count == 0){
echo "<script>
alert('No Result!.');
window.location.href='Index.php';
</script>";
} else if (empty($term)){
echo "<script>
alert('Please fill up.');
window.location.href='Index.php';
</script>";
} else {
$select_all_hospitals_query = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($select_all_hospitals_query)){
$post_hname = $row['hname'];
$post_address = $row['Address'];
$post_lat = $row['lat'];
$post_longi = $row['lng'];
$coordinates = 'new google.maps.LatLng( '. $row['lat']. ','. $row['lng']. '),';
}
}
}
?>
<html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>LabSeek</title>
<link rel="canonical" href="https://getbootstrap.com/docs/5.0/examples/sidebars/">
<link rel="stylesheet" type="text/css" href="map/map.css" />
<link href="styles/sidebars.css" rel="stylesheet">
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<!-- <script src="map/map.js"></script> -->
</head>
<body>
<div id="map">
<!-- Async script executes immediately and must be after any DOM elements used in callback. -->
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBQbnrTvBW0ML5bVlgsgMhYCqdRR04d7k0&callback=initMap&libraries=&v=weekly"
async
></script>
<script>
function initMap() {
var options = {
zoom:14,
center: { lat: 14.586225342331847, lng: 120.99824317301842 }
}
//new map
var map = new
google.maps.Map(document.getElementById('map'),options);
//add marker from search
function addMarker(coords){
var marker = new google.maps.Marker({
position: coords,map:map, });
}
addMarker({<?php echo 'lat:'. $post_lat .', lng:'. $post_longi; ?>});
}
</script>
</div>
</html>
【问题讨论】:
-
有些标记有点乱 - 2 个
html元素,div元素中的脚本...您还覆盖了 php 代码中的变量 -
为什么要运行相同的 SQL 查询两次?
标签: javascript php arrays google-maps mysqli