2012-02-13 137 views
1

I have an array of coordinates (latitude and longitude) in one .php script and want to pass these values to maps.php which will display a google map and plot these values passed (i.e. latitude and longitude coordinates) on the map.使用<a href= > tag

My question is that, is it possible to pass these values to maps.php when clicking on <a href="maps.php"> view map </a> ???

Thanks

+2

'view map' – aroth 2012-02-13 22:40:09

+3

我不明白为什么这是被拒绝的,除非它是由于OP不先搜索 – Drewdin 2012-02-13 22:42:29

回答

2

您可以序列化您的数组,然后将其传递给GET参数。例如:

# Your array of coordinates 
$coord_array = array(); 

# Serialize the coordinates 
$coord_array = serialize(coord_array); 

# In your href you'd have 
print '<a href="maps.php?coords=' . $coord_array . '">View Map</a>'; 

现在您的maps.php,你需要反序列化,你可以像往常一样与阵列交互:

# Get the information from the URL 
$coord_array = $_GET['coords']; 

# Unserialize 
$coord_array = unserialize(coord_array); 

# Check the input to make sure it hasn't been changed . . . 

# Now interact as you normally would with the array 
print_r($coord_array); 

这种方法将是理想的路过很多在单个GET参数中的值。如果您只传递一个值,那么只需设置两个GET参数(一个用于经度,一个用于纬度)可能会更好。

另外请注意,我没有测试任何代码,因为我没有时间,但概念应该是正确的。

+0

对不起,我忘了提及我有两个这些坐标的数组。一个纬度和一个经度。那么我将如何构建这个href?谢谢 – user1135192 2012-02-14 21:03:52

+0

@ uesr1135192如果我是你,我会有一系列的坐标。也就是数组(coord1,coord2,...); coord1 =数组(经度,纬度)。然而,使用您的设置,您可以序列化经度数组和纬度数组,并且只有两个GET参数(一个用于经度数组,另一个用于纬度数组)。 – Julio 2012-02-14 22:38:04

+0

感谢@Louis它的工作原理!我现在正在尝试传递第三个数组数组,该数组的名称与经过的纬度和经度坐标有关。我应该序列化和反序列化** String Array **吗?还是应该通过调用字符串数组变量直接传递给href? 此外,当我尝试序列化它时,我得到以下错误: '注意:unserialize():错误在5字节的偏移量0' 当我不序列化它时,我得到以下错误: '致命错误:不能使用字符串偏移作为数组' 谢谢 – user1135192 2012-03-02 21:58:00

0

In your HTML do this:

<a href="maps.php?param1=value1&amp;param2=value2">view map</a> 

In your PHP code you can get the values like this:

$param1 = $_GET['param1']; 
$param2 = $_GET['param2']; 

Read more on GET parameters in the PHP documentation传递值。

+2

这是无效的。 '&param2'不是标准字符引用。你的意思是'&'! – Quentin 2012-02-13 22:43:45

+0

听起来像OP想要在URL中传递多个坐标。你的方法可能不会很好地扩展。 – Julio 2012-02-13 22:48:12