从PHP数组中Post多个值

时间:2021-02-04 13:21:33

I am programming a fantasy pet game. Currently, I am working on a tool to help users manage their personal shop inventory. The tool includes options to change the sale price of each item - and that's where my problem is. Here, let me show you:

我正在编写一个奇幻的宠物游戏。目前,我正在开发一个工具来帮助用户管理他们的个人商店库存。这个工具包含了改变每件商品售价的选项——这就是我的问题所在。在这里,让我向你们展示:

The HTML

HTML

<form method="post">
<input type="text" name="cost[$item_id]" value="" />
<input type="submit" name="submit" value="Submit" />
</form>

$item_id is a variable that corresponds to the item which is being edited. It must be included in the array, otherwise... I won't know which price corresponds to what item.

$item_id是与正在编辑的项相对应的变量。它必须包含在数组中,否则……我不知道哪个价格对应哪个项目。

Also, this script:

另外,这个脚本:

<input type="text" name="cost[$item_id]" value="" />

is repeated for the amount of items in the shop's inventory. So, the form can include an unlimited amount of these entries. Hence the array.

是重复的数量项目在商店的库存。因此,表单可以包含无限数量的这些条目。因此,数组。

The Script

这个脚本

if(isset($_POST['update'])) {

foreach ($_POST['cost'] as $key => $value) {
$submitted_array = array_keys($_POST['cost']); 
echo ($_POST['cost'][$submitted_array[0]] . " " . $submitted_array[0]); 
}

}

The script above works... however, it will only print the first set of data from the array.

上面的脚本工作……但是,它将只打印来自数组的第一组数据。

Now, that seems fine - BUT, remember how I said I could have an unlimited amount of cost entries? That's my problem - when I use a variable to replace the "0" in the script above, the function stops working all together. I could repeat the function, if I knew how many entries total the array would include - but I don't. So what can I do? Is there a simple solution to my problem?

这看起来很好,但是,还记得我说过我可以有无限的成本项吗?这就是我的问题——当我使用一个变量替换上面脚本中的“0”时,函数将停止工作。我可以重复这个函数,如果我知道这个数组总共包含多少个元素——但是我不知道。那我该怎么办呢?我的问题有简单的解决办法吗?

Adding a hidden field to the form that specifies how many items are being changed is possible - I just don't know if it would be helpful.

在表单中添加一个隐藏字段来指定要修改多少项是可能的——我只是不知道这是否有用。

If you would like to see a screen caption of the form to get a better understanding of what I'm talking about, just ask and you will receive!

如果您想看到表单的屏幕说明,以便更好地理解我所谈论的内容,那么您只需询问,您就会收到!

1 个解决方案

#1


5  

That's because you're rewriting the $submitted_array variable multiple times, when you only need once.

这是因为您需要多次重写$submitted_array变量,而您只需要一次。

if (isset($_POST['update'])) {
    $submitted_array = array_keys($_POST['cost']);
    foreach ($_POST['cost'] as $key => $value) {
        echo ($_POST['cost'][$submitted_array[0]] . " " . $submitted_array[0]);
    }
}

Also, considering your cost array, you don't need to use array_keys at all:

另外,考虑到您的成本数组,您根本不需要使用array_keys:

if (isset($_POST['update'])) {
    foreach ($_POST['cost'] as $item_ID => $item_cost) {
        echo ($item_ID . " " . $item_cost);
    }
}

#1


5  

That's because you're rewriting the $submitted_array variable multiple times, when you only need once.

这是因为您需要多次重写$submitted_array变量,而您只需要一次。

if (isset($_POST['update'])) {
    $submitted_array = array_keys($_POST['cost']);
    foreach ($_POST['cost'] as $key => $value) {
        echo ($_POST['cost'][$submitted_array[0]] . " " . $submitted_array[0]);
    }
}

Also, considering your cost array, you don't need to use array_keys at all:

另外,考虑到您的成本数组,您根本不需要使用array_keys:

if (isset($_POST['update'])) {
    foreach ($_POST['cost'] as $item_ID => $item_cost) {
        echo ($item_ID . " " . $item_cost);
    }
}