简体   繁体   English

如何在php和html中制作乘法表

[英]How to make a multiplication table in php and html

I'm very new to this so bear with me. 我对此很陌生,请耐心等待。

I somehow need the following table to have HTML code that will create a table with two column and three rows. 我不知何故需要下表具有HTML代码,该代码将创建具有两列和三行的表。 The top left column will have the title rows, the middle will have columns, the bottom will have a submit button so that when you add numbers to the right columns ie put 10 in the row text box and 10 in the column text box and a 10 X 10 multiplication table will be created. 左上方的列将包含标题行,中间的列将包含列,底部的位置将具有“提交”按钮,以便在向右列添加数字时,即在行文本框中输入10,在列文本框中输入10,将创建10 X 10乘法表。

I have the following PHP and HTML codes, but they are not working together. 我有以下PHP和HTML代码,但它们不能一起使用。 Please Help! 请帮忙!

I thought that I could get the HTML code to work in the PHP document. 我以为可以在HTML文档中使用HTML代码。

The html code is supposed to have two text boxes, one for rows and one for columns that when you add a number to a row and a column, it will create a multiplication table that big. 该html代码应该有两个文本框,一个用于行,一个用于列,当您将数字添加到行和列时,它将创建一个很大的乘法表。

<form name="table" id="table" action="table.php" method="post">
      <h2>Table Generator</h2>
      <p>
    <label for="width">Rows:</label>
    <input type="text" name="Width" id="Width" />
      </p>
      <p>
    <label for="height">Columns:</label>
    <input type="text" name="height" id="height" />
      </p>
    <input name="sbt" type="submit" formaction="table.php" onClick="MM_validateForm ('width','','RisNum','height','','RisNum');return document.MM_returnValue" value="Calculate" />
    </form>
<?php

 $rows = $_POST ['width'];  
 $columns = $_POST ['height'];  

$width = 5;
$height = 6;

    $i=1;
   $table='<table border="1">';
    for($r=0;$r<$width;$r++)
    {
        $table .= '<tr>';
        for($c=0;$c<$height;$c++)
        {
            $table .= "<td>$i</td>";
            $i++;
        }
        $table .= '<tr>';
    }
    $table .= '</table>';
    echo $table;

?>

Your for loop should look something more like this: 您的for循环应类似于以下内容:

for($r=0; $r < $height; $r++)
{
    $table .= '<tr>';
    for($c=0; $c < $width; $c++)
    {
        $table .= "<td>" . ($r * $c) . "</td>";
    }
    $table .= '</tr>';
}

For a start, you should be looping through the height first (while you create the rows), then the width (for the columns). 首先,您应该先遍历高度(在创建行时),然后遍历宽度(对于列)。 Secondly, you weren't multiplying the two values, you were only outputting the width loop veriable, so you should be outputting the result of multiplying both ( $r * $c ). 其次,您没有将两个值相乘,只输出了veriable的width循环,因此您应该输出两个值相乘的结果( $r * $c )。 Also, you don't need another variable in the loop (you had $i). 另外,循环中不需要其他变量(您有$ i)。

Lastly, you weren't closing the tag, you were opening a new one at the end of the loop. 最后,您没有关闭标签,而是在循环结束时打开了一个新标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM