0
votes

I'm attempting to create a form to update multiple rows of data from a table. The table has four columns ( cola, colb, colc, cold ). I am generating the form dynamically with a foreach loop in my view. At present I am using

text('cola[][cola]', $this->cola...

And on down to name and then populate the form fields. This works fine except that it returns an array of four arrays ( cola, colb, colc, cold ), so that I have all of my cola values in one array and all of my colb values in another etc.

What I would like is to return an array of each row that is submitted, so that my result would be something like

0(cola=3, colb=7, colc=2, cold=99)

So that I can access the values simply with a for each loop. I am, however, HTML remedial and cannot seem to get the name right on the form elements to accomplish this. I know that the answer is obvious, but I keep running into either the arrays I do not want or only submitting the data from the final row.

Edit for clarifications...

Use of

text('row[$this->key][cola]'

Or

text('row[$i][cola]'

with $i as an iterator results in one array with the name of $i or $this->key that only returns the last row submitted. Removing the quotes, as in

text(row[$i][cola]

Results in an undefined constant, because the string is expected.

The closest that I have come to success is

text('row['<php echo $i ?>'][cola]'

This actually names the form elements correctly, but breaks the elements themselves. They render as plain text and not as input boxes. I really am going a bit bonkers on this one.

1
Start with rows then cols? i.e. text('row[][cola]', $this->cola... - Landjea
That only submits the last row of the form. - Blind Fish
Not if you have the array brackets as part of the row name. It would return all of them since it is an array. - Landjea
Using the row brackets as part of the name, as in {text('row[][cola]'...} results in each element being assigned it's own array. So that if I have 10 rows I return 40 separate arrays. - Blind Fish
Oh, I see what you are saying. Sorry, my quick code wasn't clear. You need a counter in the $row. text('row[$incrementer][cola]' If you are in a for loop use the incrementer from that. Otherwise add a new $i++ to your foreach loop. So after your fourth col, you would increase $i by one, for the next row. - Landjea

1 Answers

0
votes

'' is for string literals, you want interpolation, "".

$a = 'hi';
echo "$a there";
// hi there