我在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>
答案 0 :(得分: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>