Banjofie
Banjofie

Reputation: 21

Filling out a time table with arrays

I am making a time table for automatic generation of something like a time table you'll know from schools. I am good on the way to make it fill out my table but I am unsure how a best practice would look like for this exact purpose.

Example of what I generate right now with my code

I have in the image above added cell numbers, to show my point.What I want to do it to make the "session 2"-row run the loop with an "offset" of one, so in cell 5, 10, 15 and 20 it will be 'Lars'.

This is my code right now

$par_student = [
  'Jennifer',
  'John',
  'Jane',
  'Lars'
];

$sess_count = 0;

// for each row
for ($i = 0; $i <= $count_intervals; $i++) {


 // here is the loop that generates the session number and date/time, not relevant for my question.

// below, is where I go through the array of students.
    for ($r = 1; $r <= $count_rooms; $r++) {

      $index = $r - $i - 1;
      $sess_count++;

        if ($index >= 0) :
          echo $par_student[$index];
        endif;

    }

As you can see, right now, I skip the index foreach time, to count up, but when the loop then gets down to cell 5, I am stuck and can't seem to figure out the right approach to doing this.

I hope my question makes sense. Thanks!

Upvotes: 0

Views: 176

Answers (1)

Ken Lee
Ken Lee

Reputation: 8063

You may use the following way

          Room A    Room B     Room C     Room D
session 1 Jennifer  John       Jane       Lars
session 2 John      Jane       Lars       Jennifer
session 3 Jane      Lars       Jennifer   John
session 4 Lars      Jennifer   John       Jane

The sequence will be Jennifer > John > Jane > Lars

but when you assign from session 1 to session 8

for room A, there is no offset
for room B, there is a offset of ONE (so start with John)
for room C, there is a offset of TWO (so start with Jane)
for room D, there is a offset of THREE (so start with Lars)

So now you may adjust your for loop by using the above offset , I suggest you use 1, 2, 3, 4, 5, 6, 7, 8 for Room 1 , and say 9, 10, 11, 12, 13, 14, 15, 16 for Room 2 (instead of 1, 2, 3, 4 for Room 1, Room 2, Room 3, Room 4 respectively)

Upvotes: 1

Related Questions