PHP :: I am trying to print an 8x8 grid
I am trying to print an 8x8 grid on php can anyone check thi开发者_开发百科s code is I am doing something wrong
$row = 0;
$col = 0;
print "<form>";
while ($row <= 8){
print "<tr>";
$row++;
while ($col <= 8){
print "<td>";
print "<input type="checkbox" name="battle" value="ships">";
print "</td>";
$col++;
}
print "</tr>";
)
print "</form>";
$out = "<tr>";
for($i = 0; $i < 8*8; $i++){
if($i && $i % 8 == 0)
$out .= "</tr><tr>";
$out .= "<td>..</td>";
}
$out .= "</tr>";
echo $out;
Few mistakes. Right code should be following:
$row = 0;
print "<form>";
print "<table>";
while ($row < 8){ // Counts to 8. (from 0...7 = 8 times. 0 ... 8 = 9 times)
print "<tr>";
$row++;
$col = 0; // reset column to 0 each time printing one row.
while ($col < 8){
print "<td>";
print "<input type=\"checkbox\" name=\"battle\" value=\"ships\">";
// Add \ before " otherwise it will treat as the end of the quote.
print "</td>";
$col++;
}
print "</tr>";
}
print "</table>";
print "</form>";
Hope this helps.
Several issues.. a couple stick out:
You're using the condition $col <= 8
. This will iterate 9 times if your value starts at 0, because 0 through 8 is 9 numbers. When working with base 0 and counting up finitely, always use just <
(less than) your max as that basically makes your ending number one less, making up for the 0 base.
Your last closing parenthesis should be a closing brace. You start the while
with a {
, so you need to finish it with a }
, not )
.
Use while($row < 8)
and while($col < 8)
(instead of <=
) or else you will be printing a 9x9 table.
Use single quotes for checkbox
, battle
, and ships
. The outside quotation marks will treat the expression as "<input type=" , checkbox, " name=", battle, " value=", ships, ">"
(a syntax error) if you use double quotes on the inside.
For syntax problems, it helps to use an IDE - a good one in the browser would be ideone.com. You can enable syntax highlighting.
精彩评论