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

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.

Remy Porter

Source link

You May Also Like

Wait, who’s taking a vote on this? – People Of Walmart

Pretty sure for most of us that decision was already made for…

r/funny – This carwash pricelist!

This is a friendly reminder to read our rules. Memes, social media,…

Angels GM On Ohtani Potentially Leaving: ‘We Are The Most Incompetent Franchise In The History Of Professional Sports’

ANAHEIM, CA—Asked for his thoughts on the potential departure of impending free…

My Mom’s Friend Thought This Would Make A Great Wedding Gift

“My mom’s friend dressed as a clown for my parents’ wedding day.…