title: How to: Dynamic 7 day forecast calendar loops - PHP, MySQL
body: <p>I'm in trouble making a complex dynamic forecast calendar with a visible portion of 7 days only, with a MySQL table.</p>
  
  <p>The table structure is:</p>
  
  <h3>Table: tidal_prediction</h3>
  
  <pre>
  +----+---------+---------+----------+---------+---------+-----------+------------+
  | id | port_id | da_year | da_month | da_date | da_time | da_height | moon_phase |
  +----+---------+---------+----------+---------+---------+-----------+------------+
  |  1 |       1 |    2013 |  January |      04 |  16:57  |    3.75   |     0      |
  |  2 |       1 |    2013 |  January |      04 |  10:57  |    2.36   |    New     |
  |  3 |       1 |    2013 |  January |      04 |  08:57  |    3.35   |   Full     |
  |                                   ......                                       |
  +----+---------+---------+----------+---------+---------+-----------+------------+
  </pre>
  
  <p><em>you know it's a table with dummy data</em> :)</p>
  
  <p><hr/></p>
  
  <h3>LOOK OF THE 7 DAY FORECAST CALENDAR</h3>
  
  <p>I made the table and CSS for the calendar of the 7 day forecast like:<br/>
  <img src="https://i.stack.imgur.com/7bwfC.jpg" alt="7 day forecast calendar"></p>
  
  <p>The Basic HTML structure of the calendar is:</p>
  
  <pre><code>&lt;h4 class="tidal-year"&gt;2012&lt;/h4&gt;
  &lt;h5 class="tidal-month"&gt;December&lt;/h5&gt;
  &lt;table&gt;
     &lt;thead&gt;
        &lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;/tr&gt;
     &lt;/thead&gt;
     &lt;tbody&gt;
        &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
     &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>NOW,<br/>
  I'm struggling with the PHP loop to echo the MySQL data into the table dynamically.</p>
  
  <h2>SCENARIO</h2>
  
  <p>The scenario is: Code will fetch "today" and echo the forecast and will also show the next 6 days forecast using the table data. Each time the loop ends to a month's end, the code will end the table too. And a new <code>&lt;h5 class="tidal-month"&gt;&lt;?php echo $next_month_name; ?&gt;&lt;/h5&gt;</code> will generate with a new <code>&lt;table&gt;</code> following. And when the ending month denotes a year-end, then:</p>
  
  <pre><code>&lt;h4 class="tidal-year"&gt;&lt;?php echo $next_year; ?&gt;&lt;/h4&gt;
      &lt;h5 class="tidal-month"&gt;&lt;?php echo $next_month_name; ?&gt;&lt;/h5&gt;
      &lt;table&gt;
      ...
  </code></pre>
  
  <p>will make the new year's table.</p>
  
  <h3>SQL QUERY</h3>
  
  <pre><code>SELECT da_year,da_month,da_date,da_time,da_height,moon_phase
  FROM tidal_prediction
  WHERE port_id='1'
  GROUP BY port_id,da_year,da_month,da_date
  ORDER BY da_year,da_month,da_date,da_time ASC
  </code></pre>
  
  <p>I'm confused with the query too. Do I need a multidimensional array? How can I make one from this table? Is my grouping correct?</p>
  
  <p>And obviously, how can I make the complex PHP loop within a loop?</p>
  
  <p>The question may look like a complete dependency, but I'm really trying hard googling all the aspects and possible codes (like PHP yearly calendars, multidimentional array etc.) - and finally I'm undone. :(</p>