Pieter-Jan needed to add some features to a PHP-based site for managing student assessments. Students would complete projects, submit them, and then receive feedback from their peers. The number of peers providing feedback is variable, so the application has to manage that. Which, you might be thinking, "that sounds like not a big deal to manage," but for Pieter-Jan's predecessor, it seems like it may have been.
if($i > '0') {
$team=array($studentName1,$content1,$motivation1,$isTeamMate1);
}
if($i > '1') {
$team=array($studentName1,$content1,$motivation1,$isTeamMate1,$studentName2,$content2,$motivation2,$isTeamMate2);
}
if($i > '2') {
$team=array($studentName1,$content1,$motivation1,$isTeamMate1,$studentName2,$content2,$motivation2,$isTeamMate2,$studentName3,$content3,$motivation3,$isTeamMate3);
}
Each piece of student feedback contains four items. If two students give feedback, we create an array with 8. If three give feedback, we create an array with 12 items. There are seven more if
statements exactly like these examples here.
And of course, how do we parse this? With a handy, dandy for loop:
for ($u = 0; $u < $i; $u = $u +1) {
$v=$u+1;
$w=$u*4;
$x=($u*4)+1;
$y=($u*4)+2;
$z=($u*4)+3;
//remainder cut out
}
For any user u
, the variables $w
, $x
, $y
, and $z
will contain the index of their required fields.
Apparently, the customer didn't like that the amount of feedback was capped at ten students, and wanted the app to support any arbitrary number of students. At that request, the original developer quit the project, and this became Pieter-Jan's problem.