【问题标题】:Add data into a two-dimensional array using input fields使用输入字段将数据添加到二维数组中
【发布时间】:2013-06-11 12:22:15
【问题描述】:

我正在尝试使用可以从输入字段填充的二维数据创建一个数组。 我已经有了二维数据的数组,请参见下面的代码。

$kaarten = array
(
  array("Linksys Cisco EA2700","129,99"),
  array("Apple iPad 4","479,00"),
  array("Linksys Cisco RE1000","54,99")
);

foreach($kaarten as $subArray)
{
  echo $subArray[0]; // test
  echo $subArray[1]; // 12.99
}

但现在数据是从 php 代码中的数据加载的,我想制作它以便您可以用输入字段填充它,例如:

您从 2 个输入字段开始(1. 名称 2. 价格)。完成填充后,将有 2 个按钮 1. 发送或 2. 再添加 1 个,如果您按添加 1 个,则会出现 2 个输入字段,以便您可以在数组中输入更多数据。

我希望有人可以帮助我解决我的问题。

或者如果有人有更好的解决方案,请不要犹豫告诉我。

【问题讨论】:

    标签: php arrays input add


    【解决方案1】:
    <form action="" method="post">
    <input type="hidden" name="action" id="action" value="process" />
    Name: <input type="text" name="itemname" id="itemname" /><br />
    Value: <input type="text" name="itemvalue" id="itemvalue" /><br />
    <input type="submit" value="Add Item" />
    </form>
    <?php
    $kaarten = array();
    (
      array("Linksys Cisco EA2700","129,99"),
      array("Apple iPad 4","479,00"),
      array("Linksys Cisco RE1000","54,99")
    );
    
    if ($_POST['action']=="process") {
    //add item to array now we have to do this AFTER the array has been pre-populated and created
    $itemname = $_POST['itemname'];
    $itemvalue= $_POST['itemvalue'];
    //add it to the actual array
    $kaarten[] = array($itemname,$itemvalue);
    //note there's a major issue here as you're not passing the array between form posts and so its being reset every post
    //need to add in facility to pass items between form posts to allow for it to be an N length array otherwise it'll only ever container a max of 4 elements the 3 pre-defined ones and the one being added
    }
    
    foreach($kaarten as $subArray)
    {
      echo $subArray[0]; // test
      echo $subArray[1]; // 12.99
    }
    ?>
    

    这是您可以查看在您自己的表单帖子之间分流数组的基础知识隐藏字段

    【讨论】:

      猜你喜欢
      • 2022-06-29
      • 2021-08-02
      • 2019-07-23
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-02
      • 2019-04-22
      相关资源
      最近更新 更多