title: php HTML timetable with messed up html cells
body: <p>I think I have completely messed up html table while creating a school timetable
  what i require is 
  <a href="http://i.stack.imgur.com/J0kbr.png" rel="nofollow">Required Timetable</a>
  But I am getting timetable like this <a href="http://i.stack.imgur.com/iRqpl.png" rel="nofollow">Incorrect Timetable</a></p>
  
  <p>My Code is</p>
  
  <pre><code>// days of week array
  $days = array( 
  1 =&gt; 'Monday', 
  2 =&gt; 'Tuesday', 
  3 =&gt; 'Wednesday', 
  4 =&gt; 'Thursday', 
  5 =&gt; 'Friday', 
  6 =&gt; 'Saturday', 
  7 =&gt; 'Sunday' );
  
  //Selecting all the hours from lectures
   $hours = DB::table('timetablelectures')-&gt;select('timetablelectures.id', 'timetablelectures.start_time')
                                  -&gt;get();
  
  $timetable = DB::table('timetable')-&gt;select('timetable.id', 'timetable.day', 'timetable.lecture_id', 'timetable.subject_id')
                                  -&gt;get();
  
  echo "&lt;table&gt;";
  echo "&lt;tr&gt;";
  
  echo '&lt;td&gt;&lt;/td&gt;'; // empty cell
  
  foreach( $hours as $hh ) {
  
      echo "&lt;td&gt;";    
      echo $hh-&gt;start_time;
      echo "&lt;/td&gt;";
  
  }
  
  echo "&lt;/tr&gt;";
  
  foreach( $hours as $hour ) {
  
      foreach( $days as $day =&gt; $day_name ) {
  
          echo "&lt;tr&gt;";
          echo '&lt;td&gt;', $day_name, '&lt;/td&gt;'; // day of the week
  
          foreach( $timetable as $tt ) {
  
              echo "&lt;td&gt;";
  
              if( (int)$tt-&gt;day == $day and $tt-&gt;lecture_id == $hour-&gt;id ) {
                  echo $tt-&gt;subject_id;
              }
  
              echo "&lt;/td&gt;";
          }
  
          echo "&lt;/tr&gt;";
      }
  }
  echo "&lt;/table&gt;";
  </code></pre>
  
  <p>Can anyone please look into it as I have been banging my head. Also it would be great that empty cells will be shown as "Unassigned"</p>