Jump to content

Recommended Posts

Posted

I have an application where a repeater called 'newsletterrepeater' has field like this

$newsletterrepeater = $page->newsletterrepeater->sort('-date');
  foreach($newsletterrepeater as $n){
      echo $n->date.$n->title.$n->file;
  }

The field types are:

  • date = dateTime field
  • title = text field
  • file = file field

I want to group the entries into months.

See code below where [[ ]] is my unknown code.

foreach ($newsletterrepeater as $n) {

[[ foreach month ]]

<table>
  <tr>
    <th colspan="3"> [[month]] </th>
  </tr>
 	echo '
 		<tr>
 			<td>'.$n->date.'</td>
 			<td>'.$n->title.'</td>
 			<td><a target="_blank" href="'.$n->file->url.'">Download Newsletter</a></td>
 		</tr>';
</table>
 }

 

Posted

If months are in order then can't you just echo the month only if it's different from the previous month?

Posted

Thanks tpr but there are multiple rows with dates containing the same month. What i need to do is group the rows by month.

For example 

August 2016 - list of repeater rows

July 2016 - list of repeater rows etc

Posted

you can group by months by creating an array of all the used months:

$months = array();
foreach ($newsletterrepeater as $item) {
    $months[]= date("m", $item->getUnformatted("date"));
}
$months = array_unique($months);
rsort($months);

to get your month name you can do something like this:

$dateObj   = DateTime::createFromFormat('!m', $month);
$monthName = $dateObj->format('F');

but you probably need to do 2 loops where you do the years first and then the months within the years..

Posted

Hi Macrura thanks for that. My PHP is sketchy but I am nearly there.

I managed to echo the months but cannot seem to figure out how to loop the fields for each month.

For example all the repeater rows  that have August as a date in a loop. See my attempt below:

$months = array();
$newsletterrepeater = $thispage->newsletterrepeater;
	foreach ($newsletterrepeater as $n) {
		$months[] = date("m",$n->getUnformatted("newsletterdate"));
	}

$months = array_unique($months);
rsort($months);

foreach ($months as $m) {
	$monthName = date('F', mktime(0, 0, 0, $m, 10));
    echo '
    <table>
      <tr>
        <th colspan="3">'.$monthName.'</th>
      </tr>';
          
      // Stuck on this loop
      foreach ($m as $archivenews) {
          echo '<tr>
      		<td>'.$archivenews->newsletterdate.'</td>'
      		<td>'.$archivenews->newslettertitle.'</td>
      		<td><a href="'.$archivenews->newsletterlink->url.'">View File</a></td>';
          }	
      } ?>
  

 

Posted

you have to find the repeater events for the month, so you first have to apply a runtime property to each repeater event for the the month...

$newsletterrepeater = $thispage->newsletterrepeater;

$months = array();
foreach($newsletterrepeater as $n) {
    $months[] = date("m",$n->getUnformatted("newsletterdate"));
    $n->month = date("m", $n->getUnformatted("newsletterdate"));
}

$months = array_unique($months);
rsort($months);

foreach($months as $m) {
	 $monthName = date('F', mktime(0, 0, 0, $m, 10));
   $monthNews = $newsletterrepeater->find("month=$m");
    echo '<table><tr><th colspan="3">'.$monthName.'</th></tr>';
    foreach ($monthNews as $archivenews) {
        echo '<tr><td>'.$archivenews->newsletterdate.'</td>';
    		echo '<td>'.$archivenews->newslettertitle.'</td>';
    		echo '<td><a href="'.$archivenews->newsletterlink->url.'">View File</a></td>';
    }
}

 

  • Like 3
Posted

Thanks Macrura, awesome help.

A wee typo in your code

Line 6 should be 

$n->month = date("m", $n->getUnformatted("newsletterdate"));

Cheers

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...