Re-populating form with 2 dimensional indexed array - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Re-populating form with 2 dimensional indexed array (/showthread.php?tid=49502) |
Re-populating form with 2 dimensional indexed array - El Forum - 02-22-2012 [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', Now, how can I repopulate the form using set_value()? I already can validate the data using a callback like this: Code: array( 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. Re-populating form with 2 dimensional indexed array - El Forum - 02-22-2012 [eluser]meigwilym[/eluser] Assuming your fieldsets are identical: Code: $i = 0; Or something like that, depending on the exact situation. Mei Re-populating form with 2 dimensional indexed array - El Forum - 02-22-2012 [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. Re-populating form with 2 dimensional indexed array - El Forum - 02-22-2012 [eluser]meigwilym[/eluser] I see what you mean. I tried a quick test with this Code: <form method="post" action=""> 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 Re-populating form with 2 dimensional indexed array - El Forum - 02-22-2012 [eluser]20000RPM[/eluser] I think that this: Code: <input type="text" name="expenses[][name]" value="" /> gives you an array like this: Code: expenses[0] => array('name'=>''), Which doesn't keep them together if you see what I mean. Re-populating form with 2 dimensional indexed array - El Forum - 02-22-2012 [eluser]meigwilym[/eluser] Ok, but take it to the next step and Code: <input type="text" name="expenses[1][name]" value="" /> works! Code: array(1) { Mei Re-populating form with 2 dimensional indexed array - El Forum - 02-22-2012 [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! |