2011-06-06 53 views
2

我有一個地圖上有一個網格。網格中的每個單元格將通過圖像地圖座標在地圖上成爲可點擊區域。我想動態地做到這一點,以節省編寫圖像映射的所有代碼,但我不能完全弄清楚我需要使用的語句或等式。 下面是我對目前爲止的圖像地圖的一個例子。php動態生成圖像地圖座標

<div style="text-align:center;" id="location_title">The Hearth Lands</div> 
         <img id="map1" src="./images/maps/regions/1.png" usemap="#map1" border="0" width="800" height="800" alt="" /> 
         <map name="map1" id="_map1"> 
          <area shape="rect" coords="0,0,50,50" href="" alt="" title="" /> 
          <area shape="rect" coords="50,0,100,50" href="" alt="" title="" /> 
          <area shape="rect" coords="100,0,150,50" href="" alt="" title="" /> 
          <area shape="rect" coords="150,0,200,50" href="" alt="" title="" /> 
          <area shape="rect" coords="200,0,250,50" href="" alt="" title="" /> 
          <area shape="rect" coords="250,0,300,50" href="" alt="" title="" /> 
          <area shape="rect" coords="300,0,350,50" href="" alt="" title="" /> 
          <area shape="rect" coords="350,0,400,50" href="" alt="" title="" /> 
          <area shape="rect" coords="400,0,450,50" href="" alt="" title="" /> 
          <area shape="rect" coords="450,0,500,50" href="" alt="" title="" /> 
          <area shape="rect" coords="500,0,550,50" href="" alt="" title="" /> 
          <area shape="rect" coords="550,0,600,50" href="" alt="" title="" /> 
          <area shape="rect" coords="600,0,650,50" href="" alt="" title="" /> 
          <area shape="rect" coords="650,0,700,50" href="" alt="" title="" /> 
          <area shape="rect" coords="700,0,750,50" href="" alt="" title="" /> 
          <area shape="rect" coords="750,0,800,50" href="" alt="" title="" /> 

          <area shape="rect" coords="0,50,50,100" href="" alt="" title="" /> 
          <area shape="rect" coords="50,50,100,100" href="" alt="" title="" /> 
          <area shape="rect" coords="100,50,150,100" href="" alt="" title="" /> 
          <area shape="rect" coords="150,50,200,100" href="" alt="" title="" /> 
          <area shape="rect" coords="200,50,250,100" href="" alt="" title="" /> 
          <area shape="rect" coords="250,50,300,100" href="" alt="" title="" /> 
          <area shape="rect" coords="300,50,350,100" href="" alt="" title="" /> 
          <area shape="rect" coords="350,50,400,100" href="" alt="" title="" /> 
          <area shape="rect" coords="400,50,450,100" href="" alt="" title="" /> 
          <area shape="rect" coords="450,50,500,100" href="" alt="" title="" /> 
          <area shape="rect" coords="500,50,550,100" href="" alt="" title="" /> 
          <area shape="rect" coords="550,50,600,100" href="" alt="" title="" /> 
          <area shape="rect" coords="600,50,650,100" href="" alt="" title="" /> 
          <area shape="rect" coords="650,50,700,100" href="" alt="" title="" /> 
          <area shape="rect" coords="700,50,750,100" href="" alt="" title="" /> 
          <area shape="rect" coords="750,50,800,100" href="" alt="" title="" /> 

          <area shape="rect" coords="0,100,50,150" href="" alt="" title="" /> 
          <area shape="rect" coords="50,100,100,150" href="" alt="" title="" /> 
          <area shape="rect" coords="100,100,150,150" href="" alt="" title="" /> 
          <area shape="rect" coords="150,100,200,150" href="" alt="" title="" /> 
          <area shape="rect" coords="200,100,250,150" href="" alt="" title="" /> 
          <area shape="rect" coords="250,100,300,150" href="" alt="" title="" /> 
          <area shape="rect" coords="300,100,350,150" href="" alt="" title="" /> 
          <area shape="rect" coords="350,100,400,150" href="" alt="" title="" /> 
          <area shape="rect" coords="400,100,450,150" href="" alt="" title="" /> 
          <area shape="rect" coords="450,100,500,150" href="" alt="" title="" /> 
          <area shape="rect" coords="500,100,550,150" href="" alt="" title="" /> 
          <area shape="rect" coords="550,100,600,150" href="" alt="" title="" /> 
          <area shape="rect" coords="600,100,650,150" href="" alt="" title="" /> 
          <area shape="rect" coords="650,100,700,150" href="" alt="" title="" /> 
          <area shape="rect" coords="700,100,750,150" href="" alt="" title="" /> 
          <area shape="rect" coords="750,100,800,150" href="" alt="" title="" /> 

          <area shape="rect" coords="0,150,50,200" href="" alt="" title="" /> 
          <area shape="rect" coords="50,150,100,200" href="" alt="" title="" /> 
          <area shape="rect" coords="100,150,150,200" href="" alt="" title="" /> 
          <area shape="rect" coords="150,150,200,200" href="" alt="" title="" /> 
          <area shape="rect" coords="200,150,250,200" href="" alt="" title="" /> 
          <area shape="rect" coords="250,150,300,200" href="" alt="" title="" /> 
          <area shape="rect" coords="300,150,350,200" href="" alt="" title="" /> 
          <area shape="rect" coords="350,150,400,200" href="" alt="" title="" /> 
          <area shape="rect" coords="400,150,450,200" href="" alt="" title="" /> 
          <area shape="rect" coords="450,150,500,200" href="" alt="" title="" /> 
          <area shape="rect" coords="500,150,550,200" href="" alt="" title="" /> 
          <area shape="rect" coords="550,150,600,200" href="" alt="" title="" /> 
          <area shape="rect" coords="600,150,650,200" href="" alt="" title="" /> 
          <area shape="rect" coords="650,150,700,200" href="" alt="" title="" /> 
          <area shape="rect" coords="700,150,750,200" href="" alt="" title="" /> 
          <area shape="rect" coords="750,150,800,200" href="" alt="" title="" /> 

          <area shape="rect" coords="0,200,50,250" href="" alt="" title="" /> 
          <area shape="rect" coords="50,200,100,250" href="" alt="" title="" /> 
          <area shape="rect" coords="100,200,150,250" href="" alt="" title="" /> 
          <area shape="rect" coords="150,200,200,250" href="" alt="" title="" /> 
          <area shape="rect" coords="200,200,250,250" href="" alt="" title="" /> 
          <area shape="rect" coords="250,200,300,250" href="" alt="" title="" /> 
          <area shape="rect" coords="300,200,350,250" href="" alt="" title="" /> 
          <area shape="rect" coords="350,200,400,250" href="" alt="" title="" /> 
          <area shape="rect" coords="400,200,450,250" href="" alt="" title="" /> 
          <area shape="rect" coords="450,200,500,250" href="" alt="" title="" /> 
          <area shape="rect" coords="500,200,550,250" href="" alt="" title="" /> 
          <area shape="rect" coords="550,200,600,250" href="" alt="" title="" /> 
          <area shape="rect" coords="600,200,650,250" href="" alt="" title="" /> 
          <area shape="rect" coords="650,200,700,250" href="" alt="" title="" /> 
          <area shape="rect" coords="700,200,750,250" href="" alt="" title="" /> 
          <area shape="rect" coords="750,200,800,250" href="" alt="" title="" /> 
         </map> 

如何用較少的代碼創建所有這些區域?

回答

7
$columns = 5; 
$rows = 5; 
$width = 50; 
$height = 50; 

for($x = 0; $x < $columns; $x++) 
{ 
    for($y = 0; $y < $rows; $y++) 
    { 
     $a = ($x * $width); 
     $b = ($y * $height); 

     $coords = array($a, $b, ($a + $width), ($b + $height)); 
     echo '<area shape="rect" coords="'.implode(',', $coords).'" href="'.$href.'" alt="'.$alt.'" title="'.$title.'" />'; 
    } 
} 

你可以看到它在這裏的行動:http://codepad.org/MMKfY1zc

+0

正是我一直在尋找!非常感謝! – chaoskreator 2011-06-06 22:06:16

+0

嗯,我也是。大聲笑... – Midas 2011-06-06 22:10:05

+0

大聲笑。你們兩個看起來像是會做同樣的工作。 KOGI's只是將我的座標實現爲JS函數的最簡單方法。感謝大家! – chaoskreator 2011-06-06 22:26:49

2
<map name="map1" id="_map1"> 
<?php 
$rows = 5; 
$cols = 16; 
$w = $h = 50; 
for ($i = 0; $i < $rows; $i++) { 
    for ($j = 0; $j < $cols; $j++) 
     echo "\t", '<area shape="rect" coords=', $j * $w , ',', $i * $h, ',', ($j + 1) * $w, ',', ($i + 1) * $h, '" href="" alt="" title="" />', "\n"; 
    echo "\n"; 
} 
?> 
</map> 

簡單的數學。 :P

+0

解析錯誤:語法錯誤!那麼所有的','多數民衆贊成不是你如何連接 – 2011-06-06 22:11:51

+0

@勞倫斯Cherone:他使用參數樣式回聲這是完全有效的PHP。請參考手冊:http://us.php.net/manual/en/function.echo.php – webbiedave 2011-06-06 22:16:04

+0

@勞倫斯:對不起,我現在修好了。這是有效的語法。我不連接,我用'''將它們分開。這是一個微型優化,因爲PHP在回顯之前不必連接它們。如果你不得不像你巨大的圖像地圖一樣呼應,那麼特別有用。 – Midas 2011-06-06 22:16:36

1

這樣的:

<?php 

$i=0; 
foreach(range(0,750,50) as $b){ 
    foreach(range(0,750,50) as $a){ 
     echo '<area shape="rect" coords="'.$a.','.$b.','.($a+50).','.($b+50).'" href="" alt="" title="" />'."\n"; 
    } 
    $i++; 
    if($i==5){break;} 
} 

?>