I have an array of dates in descending chronilogical order. As I loop through them, currently I am splitting up the string and using and comparing the current year & month to previous.
Here is the desired output (example):
<ul>
<li><h1>2012</h1>
<ul>
<li><h2>Jan</h2>
<ul>
<li>Jan 1, 2012</li>
<li>Jan 5, 2012</li>
</ul>
</li>
<li><h2>Feb</h2>
<ul>
<li>Feb 3, 2012</li>
<li>Feb 8, 2012</li>
</ul>
</li>
</ul>
</li>
<li><h1>2011</h1>
<ul>
<li><h2>Jan</h2>
<ul>
<li>Jan 2, 2011</li>
<li>Mar 2, 2011</li>
</ul>
</li>
<li><h2>Feb</h2>
<ul>
<li>Feb 9, 2011</li>
<li>Feb 20, 2011</li>
</ul>
</li>
</ul>
</li>
</ul>
This is one of my attempts:
$archiveDateHTML = '<ul id="menu">';
$c = 0;
foreach ($ArchiveDates as $k=>$value){
$dateArray = split(' ', $value);
if($currentYear !== $dateArray[2] && $c+1 < count($ArchiveDates)){
if($c > 0){ $archiveDateHTML .= '</ul>';}
$archiveDateHTML .= '<h2 class="yearTitle">'.$dateArray[2].'</h2>';
$archiveDateHTML .= '<ul class="yearList">';
$newYear = 1;
}
if($currentMonth !== $dateArray[0] && $c+1 < count($ArchiveDates)){
if($c > 0 && $newYear==0){ $archiveDateHTML .= '</ul>';}
$archiveDateHTML .= '<h3 class="monthTitle">'.$dateArray[0].'</h3>';
$archiveDateHTML .= '<ul class="monthList">';
$newYear = 0;
}
$archiveDateHTML .= $value;
$currentYear = $dateArray[2];
$currentMonth = $dateArray[0];
$c++;
}
$archiveDateHTML .= '</ul>';
There is some obvious problems, with the above code. I keep getting so close, but can't quite wrap my head around the logic. Please assist and thank you so much!