PHP表,允许用户输入高度和宽度

时间:2014-02-19 22:03:34

标签: php html

9.2

您好。我有一个9.2任务,不知道从哪里开始。 我创建了index.php,允许用户输入表的宽度和高度。 这是代码:

<html>
<head>
    <title>Assignment 9.2</title>
</head>
<body bgcolor="black" text="white">
    <form method="post" action="table.php"
        <strong>Please select an integer for the width:</strong>
        <input type="text" name="width" size="10">
        <br>
        <strong>Please select an integer for the height:
        <input type="text" name="width" size="10">
        <input type="submit" value="Submit">
    </form>
</body>
</html> 

我不知道从哪里开始制作桌子。我不希望有这个 为我完成..但只是解释从哪里开始和我需要使用的PHP代码。 再次..这是9.2,如附图所示。

1 个答案:

答案 0 :(得分:0)

好的,如果您了解html表格在代码中的布局方式,这是一件相当简单的事情......

HTML表格格式

第一步是了解如何在html ...

中列出表格

3x2表

对于3个圆柱/ 2个划线表,代码看起来像这样...

opening table tag
    opening row tag
         cell tag
         cell tag
         cell tag
    closing row tag
    opening row tag
         cell tag
         cell tag
         cell tag
    closing row tag
closing table tag

HTML

<table>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td>4</td>
        <td>5</td>
        <td>6</td>
     </tr>
</table>

我不知道您对html了解多少,但希望如果没有可用的各种资源可以解释所使用的标记,那么这应该是有意义的。

PHP代码

此时我们将跳过宽度和高度的输入(因为你已经有了代码),并假设你已按如下方式捕获它们:

$in_width  = $_POST['width'];
$in_height = $_POST['height'];

现在我们知道桌子的宽度和高度是多少,我们可以看看如何将它们放在一起:

如果您回顾上面的示例表代码,您会看到table一次呈现一行,格式为:

  • 第一行第一列
  • 第一行第二栏
  • 第一行第三栏
  • 第二行第一栏
  • ......依旧......

通过查看上面屏幕抓取中的数字,可能会更清楚。

鉴于我们知道html表的结构如何,我们可以确定我们可以使用两个循环*(第一个嵌入在第一个循环中)来产生所需的输出。

将是外部循环,将位于内部循环中 - 请参阅上面的示例html代码)

伪代码

start table
for each row {
   start row
   for each column {
       print cell
   }
   end row
}
end table

*你可以使用任何你熟悉的循环(即for或loop),他们做同样的事情

如果你想自己这样做,我建议你不要在这里阅读。 剧透跟随......



<小时/> <小时/>

扰流

两个while循环

$in_height  = $_POST['height'];   // Get table height
$in_width   = $_POST['width'];    // Get table width
$counter    = 1;                  // Start the counter at 1
$temp_width = 0;                  // Set temporary variable for width(second loop)

echo "<table>";              // Print opening tag for table
while($in_height > 0){
    echo "<tr>";             // Print opening tag for row
    $temp_width = $in_width; // Set temp_width to width of table
    while($temp_width > 0){
        echo "<td>$counter</td>";  // Print cell with counter value
        ++$counter;                // Increment counter
        --$temp_width;             // Decrement temp_width
    }
    echo "</tr>";  // Print closing tag for row
    --$in_height;  // Decrement height
}
echo "</table>";   // Print closing tag for table

两个for循环

仅供演示......

$in_height   = $_POST['height'];
$in_width    = $_POST['width'];
$counter     = 1;
$temp_width  = 0;
$temp_height = 0;


echo "<table>";
for($temp_height = 0; $temp_height < $in_height; $temp_height++) {
    echo "<tr>";
    for($temp_width = 0; $temp_width < $in_width; $temp_width++){
        echo "<td>$counter</td>";
        ++$counter;
    }
    echo "</tr>";
}
echo "</table>";

你也可以混合和匹配循环......