2

我在 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>
4

1 回答 1

0

你需要重新设计你的循环,以获得正确的坐标,

这是我为你写的一个例子

 <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>
于 2012-05-20T16:04:23.353 回答