2

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!

1

1 Answer 1

5

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

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:

if (isset($_POST['update'])) {
    foreach ($_POST['cost'] as $item_ID => $item_cost) {
        echo ($item_ID . " " . $item_cost);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried that answer, but it still only posts the first set of data. It does print X amount of times, but.. it only posts 1 set of data.
It works PERFECTLY! =D Thank you SO much for the great, and quick, help! I really appreciate it!
@Connie, please vote up and, most important, mark the answer as correct. It's a pleasure help others here. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.