[英]Cannot get the codes right in 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>
您需要重新設計循環,以獲得正確的坐標,
這是我為你寫的一個例子
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.