2012-05-20 37 views
2

我設計了一個在php中使用循環的國際象棋棋盤。董事會一切順利,但我無法將棋子放在正確的位置,因爲循環,在棋盤的每個方格中重複相同的圖片。任何人都可以向我建議我應該改變什麼。請參閱下面的我的代碼。無法在php中獲取代碼

<html> 
<head> 
<style> 
th{ 
    width:80px; 
    height:80px; 
} 
table{ 
    border: 5px solid orange; 
    border-collapse:collapse; 
} 
td{ 
    width:80px; 
    height:80px; 
} 
tr{ 
    width:80px; 
    height:80px; 
} 
h1{ 
    color:#6633FF; 
} 
</style> 

<script type="text/javascript"> 

</script> 
</head> 
<body> 
<?php 
    echo"<h1 align='center'>SAJID Chess Board</h1>"; 

    echo"<table border='1' align='center'>"; 

    //Nested For loop starts 
    for($i=1; $i<5; $i++)//this is the iteration construct starts from here 
{ 
    echo"<tr>"; 
    for ($j=7; $j>=0; $j--) // For loop for 1st row 

    { 
    if(($j%2)==0){ 
    echo"<td bgcolor='grey'>"; 
    echo"<img src='king.gif'/>"; //here is the problem, the image of king throughout the chess board     
    } 
    else{ 
    echo"<td bgcolor='white'>"; 
    } 

    echo"</td>"; 
    }//for loop ends 
    echo "</tr>"; 

    echo "<tr>"; 

    for($j=0; $j<8; $j++){ // For loop for 2nd row 

    if(($j%2)==0){ 
    echo"<td bgcolor='grey'>"; 
    } 
    else{ 
    echo"<td bgcolor='white'>"; 
    } 

    echo"</td>"; 
    }//for loop ends 

    echo "</tr>"; 

    }//nested for loop ends 

echo "</table>"; 

?> 
</body> 
</html> 
+2

你是什麼意思,「相同的圖片」?您的代碼中沒有任何圖像引用。 –

+0

是的,因爲我把它們去掉了,因爲它弄亂了棋盤。我無法將棋子照片放在正方形的正方形中。如果我把它們放在一起,那麼在棋盤的每個方格里都會重複出現一張圖像,我甚至無法擺脫這些循環。 – bleach64

+0

正在運行的代碼:http://codepad.org/8yA0BNLQ HTML輸出:http://jsfiddle.net/TJvGq/ –

回答

0

你需要重新設計你的循環,正確的座標,

下面是一個例子IVE爲你寫

<html> 
    <head> 
     <style> 
      th{ 
       width:80px; 
       height:80px; 
      } 
      table{ 
       border: 5px solid orange; 
       border-collapse:collapse; 
      } 
      td{ 
       width:80px; 
       height:80px; 
      } 
      tr{ 
       width:80px; 
       height:80px; 
      } 
      h1{ 
       color:#6633FF; 
      } 
     </style> 

     <script type="text/javascript"> 

     </script> 
    </head> 
    <body> 
     <?php 

      // Format $pictures[Row][Column]; 
      $pictures = array(); 



      //Row 1 
      $pictures[1][1] = "Castle"; 
      $pictures[1][2] = "Horse"; 
      $pictures[1][3] = "Bishop"; 
      $pictures[1][4] = "King"; 

      //Row 2 
      $pictures[2][1] = "Pawn"; 
      $pictures[2][2] = "Pawn"; 
      $pictures[2][3] = "Pawn"; 
      $pictures[2][4] = "Pawn"; 
      $pictures[2][5] = "Pawn"; 
      $pictures[2][6] = "Pawn"; 
      $pictures[2][7] = "Pawn"; 
      $pictures[2][8] = "Pawn"; 




      echo"<h1 align='center'>SAJID Chess Board</h1>"; 
      echo"<table border='1' align='center'>"; 


      for($i = 1; $i <= 8; $i++) 
      {  
       echo "<tr>"; 
       for($j = 1; $j <=8; $j++) 
       { 
        if(($i+$j)%2) 
        { 
         echo"<td bgcolor='grey'>"; 


        } 
        else 
        { 
         echo"<td bgcolor='white'>"; 
        } 


        if(isset($pictures[$i][$j])) 
         echo $pictures[$i][$j]; 

        echo "</td>"; 
       } 
       echo "</tr>"; 
      } 


      echo "</table>"; 

     ?> 
    </body> 
</html> 
+0

他們只是例子。 –

+0

你可以。或者你可以使用多維數組... –

+0

是的,你可以使用穆爾迪暗淡的陣列,如「$圖片[$我] [$ j]」 –