簡體   English   中英

我想用 PHP 創建一個表,但我不知道該怎么做

[英]I'd like to create a table with PHP, but I don't know how to do it

我必須創建一個具有以下值的表:

桌子

我猜想創建這個表,但不是用這些值。 我有一個表單,我必須輸入一個數字,例如 5,我必須創建一個包含 5 行和 5 列的表,然后我在表單中有另一個值,用於檢查數字是否是輸入數字的倍數在箱子里。

這是我的代碼:

<html>
    <head>
        <meta charset="UTF-8">
        <title>Form 1</title>
    </head>
    <body>
        <table border="1">
            <?php
            if (isset($_POST["length"]) && isset($_POST["multiples"])) {
                $multiples = $_POST["multiples"];
                $length = $_POST["length"];
                for ($i = 1; $i <= $length; $i++) {
                    echo "<tr>";
                    for ($j = 1; $j <= $length; $j++) {
                        if ($j % 2 == 0) {
                            echo "<td bgcolor=red>$j</td> ";
                        } else {
                            echo "<td bgcolor=yellow>$j</td> ";
                        }
                    }
                }
                echo "</tr>";
            } else {
                echo <<<EOT
        <form method="post" action="forms1.php">
            <label for="logitud">Side length : </label> 
            <input type="number" name="length" > 
            <label for="multiples">Multiples: </label> 
            <input type="number" name="multiples" > 
            <input type="submit" value="Send">
        </form>
EOT;
            }
            ?>
        </table>
    </body>
</html>

這是結果:

代碼結果

如果有人可以幫助我,我將不勝感激!

最終,您要做的是制作一個$length乘以$multiples的正方形,因此為了做到這一點,您需要考慮$length * $multiples

正如您所做的那樣,有一些方法可以使用內部for循環來執行此操作,但是由於您已經熟悉% (MOD),您可以稍微簡化一下。

(此外,每當您獲得分配/測試/類似的任何內容時,請忽略整個表單內容並首先處理邏輯。一旦這很好,您可以輕松地添加到表單部分。)

<table border="1">
<?php
    $multiples = 5;
    $length = 5;

    // Loop from 1 to 25 (or whatever  L * M equals)
    for ($i = 1; $i <= ($length * $multiples); $i++) {
        // We are using a one-based start, so whenever our MOD works out to one
        // here we are at the start of a row
        if (1 === $i % $multiples) {
            echo "<tr>";
        }

        // Same logic for coloring
        if (0 === $i % 2) {
            echo "<td bgcolor=red>$i</td> ";
        } else {
            echo "<td bgcolor=yellow>$i</td> ";
        }

        // Being one-based, when our MOD is zero we are at the end of a row
        if (0 === $i % $multiples) {
            echo "</tr>";
        }
    }
    ?>
</table>

(編輯)

這是一個使用兩個循環並每次計算單元格值的版本。 $k邏輯有點復雜,但希望是有意義的。

    $multiples = 5;
    $length = 5;
    for ($i = 1; $i <= $length; $i++) {
        echo "<tr>";
        for ($j = 1; $j <= $multiples; $j++) {
            $k = (($i - 1) * $length) + $j;
            if (0 === $k % 2) {
                echo "<td bgcolor=red>$k</td> ";
            } else {
                echo "<td bgcolor=yellow>$k</td> ";
            }
        }
        echo "</tr>";
    }

更多的人可能會獨立跟蹤計數器:

    $multiples = 5;
    $length = 5;
    $k = 0;
    for ($i = 1; $i <= $length; $i++) {
        echo "<tr>";
        for ($j = 1; $j <= $multiples; $j++) {
            if (0 === $k % 2) {
                echo "<td bgcolor=red>$k</td> ";
            } else {
                echo "<td bgcolor=yellow>$k</td> ";
            }
            $k++;
        }
        echo "</tr>";
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM