title: group archive in php and mysql
body: <p>I want to create an archive list like this:</p>
  
  <ul>
  <li>2016
  
  <ul>
  <li>July 2016
  
  <ul>
  <li>testing 5 of 16</li>
  <li>testing 4 of 16</li>
  <li>testing 3 of 16</li>
  <li>testing 2 of 16</li>
  </ul></li>
  <li>February 2016
  
  <ul>
  <li>testing 1 of 16</li>
  </ul></li>
  </ul></li>
  <li>2015
  
  <ul>
  <li>November 2015
  
  <ul>
  <li>testing 1 of 15</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>but it is showing like this:</p>
  
  <ul>
  <li>2016
  
  <ul>
  <li>July 2016
  
  <ul>
  <li>testing 5 of 16</li>
  <li>testing 4 of 16</li>
  <li>testing 3 of 16</li>
  <li>testing 2 of 16</li>
  </ul></li>
  <li>February 2016
  
  <ul>
  <li>testing 1 of 16</li>
  </ul></li>
  <li>November 2015
  
  <ul>
  <li>testing 1 of 15</li>
  </ul></li>
  </ul></li>
  <li>2016
  
  <ul>
  <li>July 2016
  
  <ul>
  <li>testing 5 of 16</li>
  <li>testing 4 of 16</li>
  <li>testing 3 of 16</li>
  <li>testing 2 of 16</li>
  </ul></li>
  <li>February 2016
  
  <ul>
  <li>testing 1 of 16</li>
  </ul></li>
  <li>November 2015
  
  <ul>
  <li>testing 1 of 15</li>
  </ul></li>
  </ul></li>
  <li>2015
  
  <ul>
  <li>July 2016
  
  <ul>
  <li>testing 5 of 16</li>
  <li>testing 4 of 16</li>
  <li>testing 3 of 16</li>
  <li>testing 2 of 16</li>
  </ul></li>
  <li>February 2016
  
  <ul>
  <li>testing 1 of 16</li>
  </ul></li>
  <li>November 2015
  
  <ul>
  <li>testing 1 of 15</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>This is my source code:</p>
  
  <pre><code>$query = "SELECT * FROM blogs ORDER BY date DESC";
  $resultSet = mysql_query($query);
  
  if (mysql_num_rows($resultSet)){
  $newsArray = array();
  
  echo '&lt;ul&gt;' . PHP_EOL;                                           
  
  while ($newsResult = mysql_fetch_array($resultSet)){ 
      $newDate =  $newsResult['date'] ;   
      $timePeriod = date('F  Y ',strtotime($newDate));
      $timePeriodY = date('Y',strtotime($timePeriod));
      $timePeriodM = date('F',strtotime($timePeriod));                                          
  
      /*if (!isset($newsArray[$timePeriod])){
            $newsArray[$timePeriod] = array();
      }*/
      $newsArray[$timePeriod][] = $newsResult;                                      
  }                                         
  
  //by year
  foreach ($newsArray as $timePeriod =&gt; $newsItems){
      $timePeriodY = date('Y',strtotime($timePeriod));
      echo '&lt;li&gt;&lt;strong&gt;' . $timePeriodY . '&lt;/strong&gt;' . PHP_EOL;  
      echo '&lt;ul&gt;' . PHP_EOL;
  
      //by month
      foreach ($newsArray as $timePeriod =&gt; $newsItems){
          echo '&lt;li&gt;&lt;strong&gt;' . $timePeriod . '&lt;/strong&gt;' . PHP_EOL;  
          echo '&lt;ul&gt;' . PHP_EOL;                                
  
          //news items
          foreach ($newsItems as $item){
              echo '&lt;li&gt;';
              echo '&lt;a href="'.$wwwUrl.'press-releases/'.$item["id"].'/'.$item["titlename"].'.php"&gt;'.$item["titlename"].'&lt;/a&gt;';
              echo '&lt;/li&gt;' . PHP_EOL;
          }     
  
          //end by month
          echo '&lt;/ul&gt;' . PHP_EOL; 
          echo '&lt;/li&gt;' . PHP_EOL;                   
      }
  
      //end by year
      echo '&lt;/ul&gt;' . PHP_EOL; 
      echo '&lt;/li&gt;' . PHP_EOL;                   
  }
  
  echo '&lt;li&gt;&amp;nbsp;&lt;/li&gt;' . PHP_EOL;   
  echo '&lt;/ul&gt;' . PHP_EOL; 
  } else {
  echo 'No Blog Found';
  }
  </code></pre>
  
  <p>Please help me with this and thanks in Advance.</p>