rfv123

31797167/DEMO-print-input-arrays-by-rows

Aug 4th, 2015
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.71 KB | None | 0 0
  1. <?php
  2.  
  3. include __DIR__ .'/GroupFieldMap.php';
  4.  
  5. // -------------------------------------------------------------------------
  6. echo '<pre>';
  7. print_r($_POST);
  8. echo '</pre>';
  9.  
  10. // ------------------------------31797167/class-map-POST-array-to-required-group-structure-------------------------------------------
  11. $fieldGroup  = array(array('title'),          // only one of these
  12.                       array('label', 'value'), // may be many of these
  13.                );
  14. /**
  15.  * Map GroupIdx => fields / fieldGroups
  16.  *
  17.  * The $_POST array has the indexes in the wrong order hence the
  18.  * required 'mapping' functions.
  19.  *
  20.  * I put them in a class - it is easier for maintenance.
  21.  */
  22. $map = new GroupFieldMap($_POST, $fieldGroup);
  23.  
  24.  
  25. if (!empty($_POST)) {
  26.  
  27.     $groupIdx = 0;
  28.     while ($map->groupExists($groupIdx)) {
  29.         echo '<br />', 'groupIdx: ', $groupIdx;
  30.  
  31.         echo '<br />', 'groupIdx Summary: ', $groupIdx;
  32.         // group field summary
  33.         foreach($map->fields() as $fieldIdx => $field) {
  34.             echo '<br />', 'fieldName: ', $field,
  35.                  ' count (group): ', $map->groupFieldCount($groupIdx, $field);
  36.         }
  37.  
  38.         // group field details
  39.         /*
  40.         foreach ($map->fieldGroup() as $fieldList) { // array of related fields
  41.             foreach ($fieldList as $field) {
  42.                 echo '<br />', 'fieldName: ', $field,
  43.                     ' Value: ', $map->groupFieldValue($groupIdx, $field, $fieldIdx);
  44.             }
  45.         }
  46.          *
  47.          */
  48.         $groupIdx++;
  49.     }
  50. }
  51. ?>
  52.  
  53. <form action="" method='post'>
  54. <div class="row">
  55.     <input type="text" value="T_0_0" name="title[0][]" />
  56.  
  57.     <input type="text" value="L_0_0" name="label[0][]" />
  58.     <input type="text" value="V_0_0" name="value[0][]" />
  59.  
  60.     <input type="text" value="L_0_1" name="label[0][]" />
  61.     <input type="text" value="V_0_1" name="value[0][]" />
  62.  
  63.     <input type="text" value="L_0_2" name="label[0][]" />
  64.     <input type="text" value="V_0_2" name="value[0][]" />
  65. </div>
  66. <br />
  67. <div class="row">
  68.     <input type="text" value="T_1_0" name="title[1][]" />
  69.  
  70.     <input type="text" value="L_1_0" name="label[1][]" />
  71.     <input type="text" value="V_1_0" name="value[1][]" />
  72.  
  73.     <input type="text" value="L_1_1" name="label[1][]" />
  74.     <input type="text" value="V_1_1" name="value[1][]" />
  75. </div>
  76. <br />
  77.  
  78. <div class="row">
  79.     <input type="text" value="T_2_0" name="title[2][]" />
  80.     <input type="text" value="L_2_0" name="label[2][]" />
  81.     <input type="text" value="V_2_0" name="value[2][]" />
  82. </div>
  83. <br />
  84.     <input type="submit" value="doit" name="submit" />
  85. </form>
  86.  
  87. <?php  exit; // end of processing
  88.  
  89. /* old processing
  90.     var_dump($groupIdx, isset($_POST['title'][$groupIdx]));
  91.     while (isset($_POST['title'][$groupIdx])) {
  92.  
  93.         echo '<br />', 'One complete group for groupId: ', $groupIdx;
  94.  
  95.         reset($fieldGroups); // internal iterator
  96.         while (current($fieldGroups) !== false) {
  97.  
  98.             $currentFieldGroup = current($fieldGroups);
  99.  
  100.             $currentFieldIdx = 0;
  101.             $currentFieldName = current($currentFieldGroup);
  102.  
  103.             while (isset($_POST[$currentFieldName][$groupIdx][$currentFieldIdx])) {
  104.                 foreach ($currentFieldGroup as $fieldName) {
  105.                     echo '<br />', 'GroupId: ', $groupIdx,
  106.                           ', FieldName: ', $fieldName,
  107.                           ', FieldIdx: ', $currentFieldIdx,
  108.                           ', Value: ', $_POST[$fieldName][$groupIdx][$currentFieldIdx];
  109.                 }
  110.                 $currentFieldIdx++;
  111.             }
  112.             $currentFieldGroup = next($fieldGroups);
  113.         }
  114.  
  115.        $groupIdx++; // next group to be processed
  116.     }
  117.  *
  118.  */
Advertisement
Add Comment
Please, Sign In to add comment