0

First of all I would like to say that I am very new to PHP so maybe it is a stupid question, but there we go: I have a two dropdown select tags that have the following code in html and php:

     <select>
        <option value="default">---a---</option>
        <?php for($i=1; $i<+100; $i++) { ?>
                <option value="<?php $i ?>"><?php echo $i ?></option>
        <?php } ?>
     </select>

     <select>
        <option value="default">---b---</option>
        <?php for($j=1; $j<=100; $j++) { ?>
                <option value="<?php $j ?>"><?php echo $j ?></option>
        <?php } ?>
     </select>

Then I have a table with a row and a column (well at least that has to be changed I mean):

    <table>
                <tr>
                    <th>Date</th>
                    <th>Temp</th>
                    <th>Nr a</th>
                    <th colspan="2">a 1</th>
                    <!--another th___nr a-->
                </tr>
                <tr>
                    <td colspan="3"> </td>
                    <th>something</th>
                    <th>else</th>
                    <!--another something else___nr a-->
                </tr>
                <tr>
                    <td>
                        <?php
                            //echo date(DATE_RFC822);
                            echo date('jS\, F\, Y');
                        ?>
                    </td>
                    <td>
                        <input type="number" name="temp" placeholder="temp" />
                    </td>
                    <th>b 1</th>
                    <td>
                        <input type="number" name="something" placeholder="something" />
                    </td>
                    <td>
                        <input type="number" name="else" placeholder="else" />
                    </td>
                    <!--another td___nr a-->
                </tr>
                <!--another tr___nr b-->
            </table>

What I want to do is that with the first dropdown to control the number of a's (columns) and with the second dropdown the number of b's (rows). Is this possible with PHP and if it is, how?

I was thinking of somehow to memorize the number we choose in the dropdown into a variable and with some for's to resolve it, but I don't really know how to do it, IF it is possible.

Thank you very much in advance for your time for helping me :)

4

1 回答 1

1

PHP 是一种服务器端语言,因此您必须将 (a) 和 (b) 输入发布回服务器,然后使用 PHP 构建可用于在页面上生成所需表格的相关数据

<form action="index.php" method="post">
    <!--put your select elements here-->
    <select name="columns"></select>
    <select name="rows"></select>
    <input type="submit" />
</form>

然后在PHP文件的顶部类似

<?php
    // using terniary operator here - you can google that
    // basically ensures the vars are set to 1 if there is no post from the form
    $columns = (isset($_POST['columns'])) ? $_POST['columns'] : 1;
    $rows = (isset($_POST['rows'])) ? $_POST['rows'] : 1;
?>

然后在你的桌子上

<table>
    <?php foreach ($rows as $row):?>
        <tr>
            <?php foreach ($columns as $column):?>
            <td></td>
            <?php endforeach;?>
        </tr>
    <?php endforeach;?>
</table>

你可以像 Dieter 建议的那样使用 Ajax 来增强它,但是对于纯 PHP,你需要这样的东西。

不过,它是一个非常基本的示例,除了生成一个包含 X 列和 Y 行的空 HTML 表之外,还需要充实它来完成任何事情。

编辑:这是一个应该工作的完整文件。只需将此行下面的代码保存为 test.php 并尝试一下。然后你可以修改它以完全满足你的需要。
对不起,我也犯了一个小错误,我忘记将发布的数据转换为可以在 foreach 循环中使用的范围。下面更正了

<?php
// test.php

    // using terniary operator here - you can google that
    // basically ensures the vars are set to 1 if there is no post from the form
    $c = (isset($_POST['columns'])) ? $_POST['columns'] : 1;
    $r = (isset($_POST['rows'])) ? $_POST['rows'] : 1;

    $columns = range(1, $c);
    $rows = range(1, $r);

?>


<form action="test.php" method="post">
    <!--put your select elements here-->
    <select name="rows">
        <option value="default">---a---</option>
        <?php for($i=1; $i<+100; $i++) { ?>
                <option value="<?php echo $i ?>"><?php echo $i ?></option>
        <?php } ?>
     </select>

     <select name="columns">
        <option value="default">---b---</option>
        <?php for($j=1; $j<=100; $j++) { ?>
                <option value="<?php echo $j ?>"><?php echo $j ?></option>
        <?php } ?>
     </select>
    <input type="submit" />
</form>

<table>
    <tr>
        <?php foreach ($columns as $column):?>
        <th>Temp Head</th>
        <?php endforeach;?>
    </tr>
    <?php foreach ($rows as $row):?>
    <tr>
        <?php foreach ($columns as $column):?>
        <td>Temp Data</td>
        <?php endforeach;?>
    </tr>
    <?php endforeach;?>
</table>
于 2013-04-18T11:13:06.883 回答