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
<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.
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?
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!