0

I have multidimensional arrays that are generated by dynamic form inputs that a user can add to add things like awards, jobs and education.

When I collect the array data to push it to the .csv it prints the array groupings like this FinanceWaterlooTheodore2015TreasurerTorontoGary2014.

Is it possible to mark a point where the array loop ends so it can be more legible and come out more like this FinanceWaterlooTheodore2015//TreasurerTorontoGary2014?

$unitLevelInvolvement = $_POST["unitLevelInvolvement"];

$unitInvolvementValue = "";
$i;
foreach($unitLevelInvolvement as $involvement)
{
    $i++;
    $unitInvolvementValue .= $involvement;
}
echo $unitInvolvementValue;

    <div name="unitLevelInvolvement" id="unitLevelInvolvement">
        <input type="text" class="two-lines-textbox" name="unitLevelInvolvement[]" placeholder="Position/Committee" onBlur="this.placeholder='Position/Committee'" onFocus="this.placeholder=''" onKeyUp="checkPage3()" />
        <input type="text" class="two-lines-textbox" name="unitLevelInvolvement[]" id="oectaUnit_1" placeholder="Unit" onBlur="this.placeholder='Unit'" onFocus="this.placeholder=''" onKeyUp="checkPage3()" />

        <div class="clearFix"></div>

        <input type="text" class="two-lines-textbox" name="unitLevelInvolvement[]" id="unitPresident_1" placeholder="Unit President" onBlur="this.placeholder='Unit President'" onFocus="this.placeholder=''" onKeyUp="checkPage3()" />
        <input type="date" class="two-lines-textbox" name="unitLevelInvolvement[]" id="unitYear_1" onKeyUp="checkPage3()" />
        <input type="button" value="+" onClick="addUnitInvolvement()" />
        </div>
    </div><!-- end of unit-level-involvement div-->
    <input type="submit" value="submit" />
</form><!--endForm-->
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Theodore Steiner
  • 1,553
  • 2
  • 23
  • 34
  • Could you not just do: $unitInvolvementValue .= "//$involvement"; – Mark Phillips Oct 21 '16 at 14:40
  • Nope because that will arrange like thus finance//waterloo//theodore//2015//treasurer rather than what I need – Theodore Steiner Oct 21 '16 at 14:42
  • `$unitInvolvementValue .= "//" . $involvement;` should work, although there's no reason to assign the values to a new variable just to echo it. You could just put `echo $involvement . "//";` in the foreach loop. – StephenWidom Oct 21 '16 at 14:44
  • I tried that but what it does it put // between every value. What I need are the value groups to have slashes between them so (FinanceWaterlooTheodore2015)//(TreasurerTorontoGary2014) So loop 1 collects the first set, puts a break and then loop two will collect the second set. – Theodore Steiner Oct 21 '16 at 14:45
  • isn't better to use a comma instead of slash as a delimiter? – xpuc7o Oct 21 '16 at 14:46
  • Commas are great. But please read the question. I need to collect the groups of array iterations, mark an end and collect the next loop and then print. – Theodore Steiner Oct 21 '16 at 14:49
  • @TheodoreSteiner The problem is you're not distinguishing between "groups" in your HTML. You're adding everything to `unitLevelInvolvement` array indiscriminately. The PHP code has no way of knowing when one group ends and another begins. – StephenWidom Oct 21 '16 at 14:50
  • How would I go about amending that or marking a clear delineation? – Theodore Steiner Oct 21 '16 at 14:51
  • @stephenWidom Can you help me with this or set me on the right track – Theodore Steiner Oct 21 '16 at 14:54
  • @TheodoreSteiner This should help: http://stackoverflow.com/questions/2433727/submitting-a-multidimensional-array-via-post-with-php – StephenWidom Oct 21 '16 at 14:58

2 Answers2

1

I understand you are trying to put the separator every 4 iterations (because you have 4 inputs).

Note that this answer will only work if you have exactly 4 inputs.

You could try something like this:

$unitLevelInvolvement = $_POST["unitLevelInvolvement"];

$unitInvolvementValue = "";
$i = 0;
foreach($unitLevelInvolvement as $involvement)
    {
        $unitInvolvementValue .= $involvement;
        $i++;
        if($i % 4 == 0){
          $unitInvolvementValue .= "//";
        }
    }
echo $unitInvolvementValue;
nanocv
  • 2,227
  • 2
  • 14
  • 27
0

First of all you need to create "groups" since you don't have any yet. You can do this by creating a multidimensional array with the name attribute. Your HTML could look something like this:

<div name="unitLevelInvolvement" id="unitLevelInvolvement">
    <input type="text" class="two-lines-textbox" name="unitLevelInvolvement['group1'][]" placeholder="Position/Committee" onBlur="this.placeholder='Position/Committee'" onFocus="this.placeholder=''" onKeyUp="checkPage3()" />
    <input type="text" class="two-lines-textbox" name="unitLevelInvolvement['group1'][]" id="oectaUnit_1" placeholder="Unit" onBlur="this.placeholder='Unit'" onFocus="this.placeholder=''" onKeyUp="checkPage3()" />

    <div class="clearFix"></div>

    <input type="text" class="two-lines-textbox" name="unitLevelInvolvement['group2'][]" id="unitPresident_1" placeholder="Unit President" onBlur="this.placeholder='Unit President'" onFocus="this.placeholder=''" onKeyUp="checkPage3()" />
    <input type="date" class="two-lines-textbox" name="unitLevelInvolvement['group2'][]" id="unitYear_1" onKeyUp="checkPage3()" />
    <input type="button" value="+" onClick="addUnitInvolvement()" />
    </div>
</div><!-- end of unit-level-involvement div-->

You can then iterate over the array like this:

$unitInvolvementValue = array();
foreach($unitLevelInvolvement as $group)
{
    $groupInvolvement = "";
    foreach($group as $involvement)
    {
        $groupInvolvement .= $involvement;
    }
    $unitInvolvementValue[] = $groupInvolvement;
}

echo implode("//", $unitInvolvementValue );

I'm using the implode() approach because it adds the separator only between the values and not at the beginning or the end of the string.

simon
  • 2,896
  • 1
  • 17
  • 22