大家好,我是php新手,我必须完成我正在做的php课程的作业,但是我理解它有一些困难。
基本上,我必须使用循环和字符串创建一个具有以下输出的程序:a 00000000
b 00000000
c 00000X00
d 00000000
e 00000000
f 000X0000
g 00000000
h 00000X00
12345678
",用户在开头输入一个二维字符串。基于这个字符串(坐标)在桌子上画"x"。"
非常感谢你的帮助。我想你会喜欢这样的东西。
源代码: <?php
$Y = range('a', 'h');
$X = range(1, 8);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
<div id="in">Coord X: <input type="number" min="1" max="8" name="x" value="<?php echo $X[array_rand($X)]; ?>"></div>
<div id="in">Coord Y: <input type="text" name="y" value="<?php echo $Y[array_rand($Y)]; ?>"></div>
<br /><input id="subm" type="submit" value="DRAW!">
<input name="reset" id="reset" type="submit" value="reset">
</form>
<table>
<tfoot>
<tr>
<td></td><?php foreach($X as $x) echo "<td><strong>$x</strong></td>"; ?>
</tr>
</tfoot>
<tr>
<?php
if(isset($_GET['reset']))
{
session_destroy();
header('Location: thisPage.php');
}
$coords = !empty($_GET) ? $_GET['x'].$_GET['y'] : NULL;
$_SESSION['coords'][] = $coords;
for($i = 0; $i < count($Y); $i++)
{
echo "<tr><td><strong>$Y[$i]</strong></td>";
for($j = 0; $j < count($Y); $j++)
{
if(isset($_GET) && in_array($X[$j].$Y[$i], $_SESSION['coords']))
{
echo '<td id="ex"><strong>X</strong></td>';
}
else
{
echo '<td>0</td>';
}
}
}
echo "</tr>";
?>
</tr>
</table>
我完全同意@Marc B的观点,你需要做你自己的功课。你可以在php.net网站上了解PHP数组以及如何使用它们:http://us3.php.net/manual/en/book.array.php还有这里:http://www.w3schools.com/php/php_arrays.asp
有使用循环(While, For, Foreach)自动生成多数组的方法,但您可以找出这些方法。这里有一个手动的方法:
$array = array('a'=>array(1 => 0,2 => 0,3 => 0,),'b'=>array(1 => 0,2 => 0,3 => 0,));
然后您需要构建一个html输入并使用$_GET或$_POST接收输入
使用这个输入来决定在哪里做Marc B所说的:
$array[$y][$x] = 'X';