$ _POST中的多维数组,清单表单

时间:2015-03-09 20:48:59

标签: php forms post multidimensional-array

我正在尝试制作一个嘲笑各种清单的表格。让我解释一下:

我有很多罐子。每个罐子里面都有许多果冻豆。每个豆都不一样。我可以有任意数量的罐子和豆子。我想创建一个表单:

    public static function getForm($oData) {
        <form action="<?= esc_url($_SERVER["REQUEST_URI"]); ?>" method="post" id="CountForm">
            <fieldset>

                <?php foreach ($aJars as $oJar) {
                    $aJellyBeans = JellyBeans::getJellyBeansBase($oJar->Jar_ID); ?>

                    <p><?= $oJar->JarName; ?></p>

                    <?php foreach ($aJellyBeans as $oJellyBean) { ?>

                        <input type="checkbox" name="?" id="CountedFor" value="1">

                        <input type="hidden" name="?" id="AccountedFor_ID" value="<?= $oData->AccountedFor_ID; ?>">

                        <input type="hidden" name="?" id="Jar_ID" value="<?= (($oData->Jar_ID) ? $oData->Jar_ID : $oJellyBean->Jar_ID); ?>">

                        <input type="hidden" name="?" id="JellyBean_ID" value="<?= (($oData->JellyBean_ID) ? $oData->JellyBean_ID : $oJellyBean->JellyBean_ID); ?>">

                        <label for="Notes">Notes</label>
                            <textarea name="?" id="Notes" cols="30" rows="5" wrap="soft" placeholder=" "><?= $oData->Notes; ?></textarea>
                    <?php } ?>

                <?php } ?>

                <input type="submit" name="Submit" id="Submit" value="<?= (($oData->AccountedFor_ID) ? 'Update' : 'Add' ) ?>">

            </fieldset>
            </form> <?php
    }

当$ _POST被发送时,它会进入一个处理写入数据库的类函数。该函数将查看name字段并将其用作列名。

    protected function CreateAccountedFor($aData) {
        //Process provided data
        foreach ($aData as $sKey => $eValue) {
            $aSQL[$sKey] = $eValue;
        }
    }

我想创建一个每个果冻豆的列表,按罐子排序,并且能够检查每个果冻豆,因为&#34;占了&#34;并一次性提交。

我知道名字字段是$ _POST所看到的,但有没有办法操纵该属性以形成如下数组:

    ["AccountedFor"]=>
        [0]=>
           ["AccountedFor_ID"]=> "..."
           ["Jar_ID"]=> "..."
           ["JellyBean_ID"]=> "..."               
        [1]=>
           ["AccountedFor_ID"]=> "..."
           ["Jar_ID"]=> "..."
           ["JellyBean_ID"]=> "..."

1 个答案:

答案 0 :(得分:1)

您可以在输入名称中指定数组值,使用for循环来计算当前正在进行的迭代。

如果你生成了多个这些输入,你将会有无效的代码,每个输入的id将被重复,可能会使用类而不是完全抛弃它们。

<?php for ($i = 0; $i < count($aJellyBeans); $i++) { ?>

    <input type="checkbox" name="AccountedFor[$i][CountedFor]" id="CountedFor" value="1">

    <input type="hidden" name="AccountedFor[$i][AccountedFor_ID]" id="AccountedFor_ID" value="<?= $oData->AccountedFor_ID; ?>">

    <input type="hidden" name="AccountedFor[$i][Jar_ID]" id="Jar_ID" value="<?= (($oData->Jar_ID) ? $oData->Jar_ID : $aJellyBeans[$i]->Jar_ID); ?>">

    <input type="hidden" name="AccountedFor[$i][JellyBean_ID]" id="JellyBean_ID" value="<?= (($oData->JellyBean_ID) ? $oData->JellyBean_ID : $aJellyBeans[$i]->JellyBean_ID); ?>">

    <label for="Notes">Notes</label>
        <textarea name="?" id="Notes" cols="30" rows="5" wrap="soft" placeholder=" "><?= $oData->Notes; ?></textarea>
<?php } ?>