使用两个变量连接URL
问题描述:
What I am doing is getting latitude and longitude from databases and displaying it in $row['latitude'] format. Now what I want to t to do is which ever hyperlink user clicks it takes him to maps.google.com and show location against that particular latitude and longitude.
please help! Here is my PHP code.
<?php
$location=$_POST["location"];
$myArray = json_decode($location,true);
$jsonArray = $myArray[0];
$longitude = $jsonArray['longitude']; $adress = $jsonArray['adress'];
$con=mysqli_connect("","","","");
// Check connection if (mysqli_connect_errno()) { echo "Failed to
connect to MySQL: " . mysqli_connect_error(); }
$sql = "INSERT INTO chokh_db. gpslocations ( latitude, longitude ,
datetime ) VALUES ( '$latitude' , '$longitude' , NOW() );";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con)); } $result = mysqli_query($con,"SELECT * FROM _db. gpslocations where
latitude <> 0");
while($row = mysqli_fetch_array($result)) {
$lat = $row['latitude'];
$long = $row['longitude']
echo $row['datetime'] .
" " .
$row['latitude'] .
" " .
$row['longitude'] ;
$url = "http://maps.google.com/?q='.$lat,$long.' ";
echo "<a href='$url'>View</a>"; echo "<br>";
}
mysqli_close($con);
?>
答
I don't see where you are using the $url variable that you are setting but it looks like you have a typo in setting it.
///Change this
$url = "http://maps.google.com/?q= '.$lat,$long.' ";
///To this
$url = "http://maps.google.com/?q=".$lat . "," . $long;
This should produce a url of something like..
http://maps.google.com/?q=37.3325004578,-122.03099823
答
You can simply write the url variable as below.
$url = "http://maps.google.com/?q=$lat,$long";
Above string is enclosed in double quotation marks, therefore $lat and $long variables will replaced by their values. This is similar to writing it as
$url = "http://maps.google.com/?q=".$lat.",".$long;