Theodore Steiner Theodore Steiner - 1 month ago 14
Javascript Question

PHP - Spacing out Array Iterations

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-->

Answer

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;