Okay, here's the situation:

I have a simple form that the user can sort by different categories, and adjust two parameters of each element. So for example, the form looks like:

ID: 1
Name: Dave One
Amount1: 60
Amount2: 42

ID: 2
Name: Greg One
Amount1: 33
Amount2: 15

ID: 3
Name: Greg Two
Amount1: 22
Amount2: 52

and so on...

The user can make it display Gregs only, Dave only, change the amounts and submit, but I can only get it to work when there is a single element displayed at a time ie: when I don't try to get it to accept and store as an array. The problem is that when I have multiple elements submitted I can't get the action file to understand it. Here's what I'm doing so far:

The form id's each field with the ID number, such as: amount1_$ID

so I have...

amount1_1 (Dave)
amount1_2 (Greg)
etc..

as what is being submitted.. problem is the action file can't predict how many variables it's gonna get so I don't think I can get it to work this way.

So I'm looking into arrays, but getting confused. Any help would be greatly appreciated, not looking for an easy answer, just a point in the right direction and I can figure it out from there.

I just want to know how I should be labelling my form ID's, what variables I need to send to the action file, and how I can get the action file to read it and update the appropriate fields. I feel like I need to do some kind of while function in the action file but not sure.

    Yes array is the way to go.

    When creating form, create the fields with these names($id is ofcourse the id coming from the database):

    data[$id][name]
    data[$id][amount1]
    data[$id][amount2]

    Now when you submit the form you can find all in $POST['data'] variable. With foreach you can easily update:

    if (isset($_POST['data'])) {
    	foreach($_POST['data']as $id=>$v) {
    		$sql = sprintf("UPDATE tablename SET name='%s',amount1='%s',amount2='%s' WHERE id=%d",
    		mysql_real_escape_string($v['name']),
    		mysql_real_escape_string($v['amount1']),
    		mysql_real_escape_string($v['amount2']),
    		mysql_real_escape_string($id));
    		mysql_query($sql);
    	}
    }
    

      WOOOOOOOOO GOT IT TO WORK!!! I LOVE YOU CAHVA 🙂 🙂 🙂 🙂

        You might want to normalize your database. Both using arrays in the database and using two columns for the same information (amount) makes the table not normalized, and it will cause troubles in the future.

          Write a Reply...