duanshaiduhao2471 2012-05-20 15:45
浏览 17
已采纳

无法在php中获取代码

I have designed a chess board using loops in php. The board is all well, but I cannot get the chess pieces in right position, because of loops, same picture repeats in every squares of the chess boards. Can anyone suggest me what should I change. Please refer to my codes below.

<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>
  • 写回答

1条回答 默认 最新

  • dongzhi4690 2012-05-20 16:04
    关注

    You need to redesign your loops, for the correct coordinates,

    Here is an example ive wrote for you

     <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分