Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8292567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:31:40+00:00 2026-06-08T13:31:40+00:00

I am programming a fantasy pet game. Currently, I am working on a tool

  • 0

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
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:31:42+00:00Added an answer on June 8, 2026 at 1:31 pm

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am programming an online PHP-based fantasy pet simulation game. I am not very
I am designing a RPG game like final fantasy. I have the programming part
Programming novice here. I'm trying to allow a user to enter their name, firstName
When programming through sockets in C, one can automatically get information about their interfaces
Programming Clojure is currently available only in electronic form. I see it's available in
Programming newbie here, about a month in with Ruby. I'm currently playing around with
Im programming a little game where two Players have to click some Buttons it
Programming languages had several (r)evolutionary steps in their history. Some people argue that model-driven
Programming language: Java Ok, so I want to have a BufferedImage that keeps rotating
Programming is (so far) just a hobby for me, so I try to find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.