We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37047 ☆ A M B ☆
    • 309 Posts
    I am working on a site that I didn't build, and have come across a problem with the sort order of the blog archives. (see screenshot) It is sorting the years as 2011, 2010, 2012, which makes no sense to me at all.

    The months are also sorted wrong, apparently by ignoring the leading zero, so they are: 1, 10, 11, 12, 2, 3, etc. (Jan, Oct, Nov, Dec, Feb, Mar.)

    The date that is being sorted is a TV entryDate with Input Type: Date and Widget: Unixtime.

    The site is Evo 1.0.5, Php 5.2.13

    The snippet that creates the archive is:

    <?php
    $sql = "SELECT id FROM site_content sc WHERE sc.template = 7";
    $rs = $modx->db->query($sql);
    $links = array();
    while($row = mysql_fetch_array($rs)){
    	
    	$id = $row['id'];
    	$tvs =  $modx->getTemplateVarOutput("*", $id);
    	$year = date("Y", $tvs['entryDate']);
    	$month = (int)date("n", $tvs['entryDate']);
    	$monthName = date("F", $tvs['entryDate']);
    	$links[$year][$month] = "<a href='[~4~]?&reflect_month=$month&reflect_year=$year'>" . $monthName . "</a>";
    	
    }
    
    foreach($links as $year => $months){
    	sort($months);
    	echo "<ul class=''><li><a href='[~4~]?&reflect_year=$year'>" . $year . "</a><ul class='reflect_months'>";
    	foreach($months as $month){
    		echo "<li>" . $month . "</li>";
    	}
    	echo "</ul></li></ul>";
    
    }
    
    ?>
    


    The manager is set to display dates as dd/mm/YYYY. I checked and this is how the dates for this TV are stored in the DB. Don't know if that would make a difference. [ed. note: lucy last edited this post 14 years, 7 months ago.]
      Lucy Iannotti
      Following Sea design & development
      http://www.following-sea.com
      New Bedford, MA
      • 16278
      • 928 Posts
      Your only reliable sort key here is the unixtime output from the TV, so you can create an array of that value for the items, sort it, and then split down into unique years and months. It was an interesting challenge, so I did it, separating the PHP from the HTML and getting rid of the echo statements along the way, and making it more general purpose.

      The snippet expects you to create two template chunks, an outer wrapper for the years list and an inner template for the month items. You'll need to work your Reflect parameters into the chunks and parseChunk arrays.
      :) KP
      <?php
      // ArchiveLister
      // KP Feb 2012
      // Build year + month index of items using given template
      // uses template chunks for year and month lists
      
      $template = 7;
      $inner = 'inner.tpl';
      $outer = 'outer.tpl';
      $tvDate = 'entryDate';
      
      $output = "";
      
      $content = $modx->getFullTableName('site_content');
      $sql = "SELECT id FROM $content WHERE template = $template";
      $rs = $modx->db->query($sql);
      
      $archive = array();
      // build an array of item timestamps
      while($row = mysql_fetch_array($rs)){
          $id = $row['id'];
          $tvs =  $modx->getTemplateVarOutput("*", $id);
      	$archive[] = $tvs[$tvDate];
      }
      
      sort($archive);
      
      // now select first unique values for years + months
      // build separate index for distinct years
      $months = array();
      $years = array();
      
      foreach($archive as $item) {
      	$year  = strftime('%Y', $item);
      	$month = strftime('%B', $item);
      
      	if (!in_array($year, $years)) {
      		$years[] = $year;
      		$months[$year][] = $month;
      	}
      
      	if (!in_array($month, $months[$year])) {
      		$months[$year][] = $month;
      	}
      }
      
      // create the hyperlink lists
      // build list of months for each year, then insert into year list template
      foreach ($years as $listYear) {
      	$monthList = "";
      	$yearLinks = $months[$listYear];
      
      	foreach ($yearLinks as $linkMonth) {
      		$monthList .= $modx->parseChunk($inner, array('month'=>$linkMonth), '[+', '+]');
      	}
      
      	$yearList = array('year'=>$listYear, 'months'=>$monthList);
      	$output .= $modx->parseChunk($outer, $yearList, '[+', '+]');
      }
      
      return $output;
      ?>


      Basic inner template:
      <li>[+month+]</li>

      and outer:
      <ul>
      <li>[+year+]
        <ul>[+months+]</ul>
      </li>
      </ul>
        • 37047 ☆ A M B ☆
        • 309 Posts
        Wow, this is above and beyond--thank you for this! I will give it a go today.

        I am not sure how I work the reflect parameters into the chunks and parseChunk arrays--would you be able to elaborate on this?
          Lucy Iannotti
          Following Sea design & development
          http://www.following-sea.com
          New Bedford, MA
          • 16278
          • 928 Posts
          I've never strayed into Reflect territory, but you try it like this:
          Replace lines 53-55 with
          	foreach ($yearLinks as $linkMonth) {
          		$thisMonth = array('year'=>$listYear, 'month'=>$linkMonth);
          		$monthList .= $modx->parseChunk($inner, $thisMonth, '[+', '+]');
          	}
          

          inner.tpl:
          <li><a href="[~4~]?reflect_month=[+month+]&reflect_year=[+year+]">[+month+]</li>

          outer.tpl:
          <ul>
          <li><a href="[~4~]?reflect_year=[+year+]">[+year+]</a>
            <ul>[+months+]</ul>
          </li>
          </ul>

          ;) KP