Welcome Guest, Not a member yet? Register   Sign In
Re-populating form with 2 dimensional indexed array
#1

[eluser]20000RPM[/eluser]
Hi,

I've got a form which can have an arbitrary number of fields (in reality probably very few: less that 5).

The posted array might look like this:

Code:
expenses[0][name] => 'Travel',
expenses[0][amount] => '£5.00',
expenses[1][name] => 'Equipment',
expenses[1][amount] => '£15.00'

Now, how can I repopulate the form using set_value()? I already can validate the data using a callback like this:

Code:
array(
    'field' => 'expenses[]',
    'label' => 'Expenses',
    'rules' => 'callback__is_expense'
)

And that callback can return the array properly. But it's not repopulating the form. I'm doing this:

Code:
set_value('expenses[0][amount]');

I could take a different approach if that's the way to go.
#2

[eluser]meigwilym[/eluser]
Assuming your fieldsets are identical:

Code:
$i = 0;
foreach($expenses as $e)
{
    echo '<input type="text" name="name[$i]" value="'.set_value($e['name']).'" />;
    echo '<input type="text" name="amount[$i]" value="'.set_value($e['amount']).'" />;
    $i++;
}

Or something like that, depending on the exact situation.

Mei
#3

[eluser]20000RPM[/eluser]
Ok, the thing with that method is that it relies on 2 'synchronised' indexed arrays (name[] and amount[]) rather than one 2-dimensional array. That feels a little messy to me, but I may yet have to go down that road. I was hoping to keep the related properties in the same array.
#4

[eluser]meigwilym[/eluser]
I see what you mean.

I tried a quick test with this
Code:
<form method="post" action="">

<input type="text" name="expenses[]name[]" value="" />
<input type="text" name="expenses[]amount[]" value="" />

<input type="text" name="expenses[]name[]" value="" />
<input type="text" name="expenses[]amount[]" value="" />

<input type="text" name="expenses[]name[]" value="" />
<input type="text" name="expenses[]amount[]" value="" />

<input type="text" name="expenses[]name[]" value="" />
<input type="text" name="expenses[]amount[]" value="" />

<input type="text" name="expenses[]name[]" value="" />
<input type="text" name="expenses[]amount[]" value="" />

<input type="text" name="expenses[]name[]" value="" />
<input type="text" name="expenses[]amount[]" value="" />

<input type="submit">
</form>

but $_POST just gives and expenses array (I also tried without the second pair of square brackets).

Sorry, but I can't help you here, although I'm interested to see what others have to say.

Mei
#5

[eluser]20000RPM[/eluser]
I think that this:

Code:
<input type="text" name="expenses[][name]" value="" />
<input type="text" name="expenses[][amount]" value="" />

gives you an array like this:

Code:
expenses[0] => array('name'=>''),
expenses[1] => array('amount'=>'')

Which doesn't keep them together if you see what I mean.
#6

[eluser]meigwilym[/eluser]
Ok, but take it to the next step and
Code:
<input type="text" name="expenses[1][name]" value="" />
<input type="text" name="expenses[1][amount]" value="" />

<input type="text" name="expenses[2][name]" value="" />
<input type="text" name="expenses[2][amount]" value="" />

<input type="text" name="expenses[3][name]" value="" />
<input type="text" name="expenses[3][amount]" value="" />

<input type="text" name="expenses[4][name]" value="" />
<input type="text" name="expenses[4][amount]" value="" />

<input type="text" name="expenses[5][name]" value="" />
<input type="text" name="expenses[5][amount]" value="" />

<input type="text" name="expenses[6][name]" value="" />
<input type="text" name="expenses[6][amount]" value="" />

works!
Code:
array(1) {
  ["expenses"]=>
  array(6) {
    [1]=>
    array(2) {
      ["name"]=>
      string(1) "1"
      ["amount"]=>
      string(1) "2"
    }
    [2]=>
    array(2) {
      ["name"]=>
      string(1) "3"
      ["amount"]=>
      string(1) "4"
    }
    [3]=>
    array(2) {
      ["name"]=>
      string(1) "5"
      ["amount"]=>
      string(2) "67"
    }
    [4]=>
    array(2) {
      ["name"]=>
      string(2) "89"
      ["amount"]=>
      string(1) "9"
    }
    [5]=>
    array(2) {
      ["name"]=>
      string(1) "1"
      ["amount"]=>
      string(1) "2"
    }
    [6]=>
    array(2) {
      ["name"]=>
      string(1) "3"
      ["amount"]=>
      string(1) "4"
    }
  }
}

Mei
#7

[eluser]20000RPM[/eluser]
Yes, that works, but I don't know exacly how many pairs of fields I'll be using. Could be zero, could be 4. I've gone the way of running 2 parallel arrays now, though, even though it wasn't my preferred route. Gotta get the job out the door!




Theme © iAndrew 2016 - Forum software by © MyBB