phpfor-loopforeachkirby

Foreach: Group same data


I am attempting to group generated data from a foreach statement by date. I am using Kirby CMS but am having troubling accomplishing the following.

Goal (sudo):

date: 03/10/15
Take Over

date: 03/09/15
Video
Flash
Super GIF 2
Super GIF
Image

My Attempt:

date: 03/10/15
Take Over
date: 03/09/15
Video
date: 03/09/15
Flash
date: 03/09/15
Super GIF 2
date: 03/09/15
Super GIF
date: 03/09/15
Image

Code:

  <ul>
    <?php foreach($page->children()->visible()->sortBy('modified', 'desc')  as $projects): ?>
      <li>
        date: <?php echo $projects->modified('m/d/y') ?> <br>
        <a href="<?php echo $projects->url() ?>"><?php echo $projects->title()->html() ?></a>
      </li>
    <?php endforeach  ?>  
  </ul>

Second unsuccessful attempt:

  <ul>
      <?php foreach($page->children()->visible()->sortBy('modified', 'desc')  as $projects): ?>
       <li>
          <?php foreach($page->children()->visible()->sortBy('modified', 'desc')  as $project): ?>

            <?php 
              if($projects->modified('m/d/y') == $project->modified('m/d/y')) {
                echo $project->modified('m/d/y') . '<br>' . $project->title();
              }

            ?>

          <?php endforeach  ?>  
       </li>


    <?php endforeach  ?>  
  </ul>

Output:

03/10/15
Take Over
03/09/15
Video 03/09/15
Flash 03/09/15
Super GIF 2 03/09/15
Super GIF 03/09/15
Image
03/09/15
Video 03/09/15
Flash 03/09/15
Super GIF 2 03/09/15
Super GIF 03/09/15
Image
03/09/15
Video 03/09/15
Flash 03/09/15
Super GIF 2 03/09/15
Super GIF 03/09/15
Image
03/09/15
Video 03/09/15
Flash 03/09/15
Super GIF 2 03/09/15
Super GIF 03/09/15
Image
03/09/15
Video 03/09/15
Flash 03/09/15
Super GIF 2 03/09/15
Super GIF 03/09/15
Image

Solution

  • Create a variable to hold the current date - $currentDate, then check if the $projects->modified('m/d/y') matches the $currentDate and if not, set it as the $currentDate and echo it-

      <?php $currentDate = ''; ?>
      <ul>
        <?php foreach($page->children()->visible()->sortBy('modified', 'desc')  as $projects): ?>
          <li>
            <?php if($projects->modified('m/d/y') != $currentDate) { $currentDate = $projects->modified('m/d/y'); ?>
            date: <?php echo $projects->modified('m/d/y') ?> <br>
            <?php } ?>
            <a href="<?php echo $projects->url() ?>"><?php echo $projects->title()->html() ?></a>
          </li>
        <?php endforeach  ?>  
    

    edited - if you want the breaks between the dates, you can close/open the <li>/</li> inside the if

      <?php $currentDate = ''; ?>
      <ul>
        <?php foreach($page->children()->visible()->sortBy('modified', 'desc')  as $projects): ?>
          <li>
    
            <?php if($projects->modified('m/d/y') != $currentDate) { 
                if($currentDate != '') { echo "</li><li>"; } // if not the 1st date, add a blank space/li before the next date 
                $currentDate = $projects->modified('m/d/y'); ?>
            date: <?php echo $projects->modified('m/d/y') ?>
            <?php } ?>
    
            <br><a href="<?php echo $projects->url() ?>"><?php echo $projects->title()->html() ?></a>
    
        <?php endforeach  ?>  
          </li>
      </ul>