2012-02-13 142 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