Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include __DIR__ .'/GroupFieldMap.php';
- // -------------------------------------------------------------------------
- echo '<pre>';
- print_r($_POST);
- echo '</pre>';
- // ------------------------------31797167/class-map-POST-array-to-required-group-structure-------------------------------------------
- $fieldGroup = array(array('title'), // only one of these
- array('label', 'value'), // may be many of these
- );
- /**
- * Map GroupIdx => fields / fieldGroups
- *
- * The $_POST array has the indexes in the wrong order hence the
- * required 'mapping' functions.
- *
- * I put them in a class - it is easier for maintenance.
- */
- $map = new GroupFieldMap($_POST, $fieldGroup);
- if (!empty($_POST)) {
- $groupIdx = 0;
- while ($map->groupExists($groupIdx)) {
- echo '<br />', 'groupIdx: ', $groupIdx;
- echo '<br />', 'groupIdx Summary: ', $groupIdx;
- // group field summary
- foreach($map->fields() as $fieldIdx => $field) {
- echo '<br />', 'fieldName: ', $field,
- ' count (group): ', $map->groupFieldCount($groupIdx, $field);
- }
- // group field details
- /*
- foreach ($map->fieldGroup() as $fieldList) { // array of related fields
- foreach ($fieldList as $field) {
- echo '<br />', 'fieldName: ', $field,
- ' Value: ', $map->groupFieldValue($groupIdx, $field, $fieldIdx);
- }
- }
- *
- */
- $groupIdx++;
- }
- }
- ?>
- <form action="" method='post'>
- <div class="row">
- <input type="text" value="T_0_0" name="title[0][]" />
- <input type="text" value="L_0_0" name="label[0][]" />
- <input type="text" value="V_0_0" name="value[0][]" />
- <input type="text" value="L_0_1" name="label[0][]" />
- <input type="text" value="V_0_1" name="value[0][]" />
- <input type="text" value="L_0_2" name="label[0][]" />
- <input type="text" value="V_0_2" name="value[0][]" />
- </div>
- <br />
- <div class="row">
- <input type="text" value="T_1_0" name="title[1][]" />
- <input type="text" value="L_1_0" name="label[1][]" />
- <input type="text" value="V_1_0" name="value[1][]" />
- <input type="text" value="L_1_1" name="label[1][]" />
- <input type="text" value="V_1_1" name="value[1][]" />
- </div>
- <br />
- <div class="row">
- <input type="text" value="T_2_0" name="title[2][]" />
- <input type="text" value="L_2_0" name="label[2][]" />
- <input type="text" value="V_2_0" name="value[2][]" />
- </div>
- <br />
- <input type="submit" value="doit" name="submit" />
- </form>
- <?php exit; // end of processing
- /* old processing
- var_dump($groupIdx, isset($_POST['title'][$groupIdx]));
- while (isset($_POST['title'][$groupIdx])) {
- echo '<br />', 'One complete group for groupId: ', $groupIdx;
- reset($fieldGroups); // internal iterator
- while (current($fieldGroups) !== false) {
- $currentFieldGroup = current($fieldGroups);
- $currentFieldIdx = 0;
- $currentFieldName = current($currentFieldGroup);
- while (isset($_POST[$currentFieldName][$groupIdx][$currentFieldIdx])) {
- foreach ($currentFieldGroup as $fieldName) {
- echo '<br />', 'GroupId: ', $groupIdx,
- ', FieldName: ', $fieldName,
- ', FieldIdx: ', $currentFieldIdx,
- ', Value: ', $_POST[$fieldName][$groupIdx][$currentFieldIdx];
- }
- $currentFieldIdx++;
- }
- $currentFieldGroup = next($fieldGroups);
- }
- $groupIdx++; // next group to be processed
- }
- *
- */
Advertisement
Add Comment
Please, Sign In to add comment