Reputation: 25765
I have a form with a multi-level tree of one to many relationships. For example:
Match -> Phase 1 -> Phase Property 1
-> Phase 1 -> Phase Property 2
-> Phase 2 -> Phase Property 1
-> Phase 2 -> Phase Property 2
-> Phase 2 -> Phase Property 3
-> Phase 3 -> Phase Property 1
-> Phase 3 -> Phase Property 2
So, on the front-end I am able to add many phases to a match and many phase properties to a phase.
In the PHP back-end, I would like to be able to represent this data in a multi-dimensional array, so that I can loop through all the phases and then loop through each phase's properties. Ideally, I would like to stay away from managing Ids/names through JavaScript.
I know I can receive an array in PHP using something like this:
<input type="text" name="phases[]" />
But, how do I continue this pattern for the properties? Can I do something like:
<input type="text" name="phaseProperties[][]" />
and then somehow "link" each property to the right phase?
Upvotes: 0
Views: 503
Reputation: 61
If the input fields appear on a static page, then you should already know how much input fields you are going to make on the server side. So why is using fields like:
<input type="text" name="phaseProperties[0][]" />
<input type="text" name="phaseProperties[0][]" />
<input type="text" name="phaseProperties[1][]" />
such a bad thing? If the fields are being dynamically (client-side) generated then there shouldn't be a problem naming them dynamically aswell. You seem to be constraining yourself too much for no apparent reason.
Upvotes: 2
Reputation: 60536
When you render the UI (HTML), you could just use php to loop over and outputs the input?
$phases = 3;
$phasesProperty = array(
array(1, 2),
array(1, 2, 3),
array(1, 2)
);
for($i = 0; $i < $phases; $i++) {
foreach($phasesProperty[$i] as $j) {
printf('<input type="text" name="Match[Phase%d][PhaseProperty%d]" />', $i, $j);
}
}
Upvotes: 0
Reputation: 44346
You can't use (actually you can, but it doesn't help you)
<input type="text" name="phaseProperties[][]" />
because PHP has no way of knowing how you want your items grouped. It will add each item into a separate group.
Array
(
[0] => Array
(
[0] => Item 1
)
[1] => Array
(
[0] => Item 2
)
[2] => Array
(
[0] => Item 3
)
[3] => Array
(
[0] => Item 4
)
)
There isn't anything special about using []
in parameters. They behave exactly as the []
operator in PHP. For example:
$arr[][] = 'Item 1';
$arr[][] = 'Item 2';
$arr[][] = 'Item 3';
$arr[][] = 'Item 4';
print_r($arr);
would have the same output I posted above.
Upvotes: 1
Reputation: 46692
Yes, you can do like:
<input type="text" name="phaseProperties[Phase1][Property1]" />
<input type="text" name="phaseProperties[Phase1][Property2]" />
<input type="text" name="phaseProperties[Phase1][Property3]" />
<input type="text" name="phaseProperties[Phase2][Property1]" />
<input type="text" name="phaseProperties[Phase2][Property2]" />
<input type="text" name="phaseProperties[Phase2][Property3]" />
And in backend PHP, you will get phaseProperties like this:
Array
(
[Phase1] => Array
(
[Property1] => a
[Property2] => b
[Property3] => c
)
[Phase1] => Array
(
[Property1] => d
[Property2] => e
[Property3] => f
)
)
Upvotes: 1