Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Right now my output is like this

2021
paper 2

2021
paper 1

But I want my output like this

2021
paper2
paper 1

Please help me with the right code.

What I have tried:

This is my code:-

 </div>
      <!-- Main Content -->
      <div class="main-content">
      <h1 class="recent-post-title"><center>Publications</center></h1><br><br>

     <?php foreach ($publication as $key => $publications): ?>

          <div class="content-publication clearfix">
          <div class="main-content single">
             <h1 class="post-title"><center><?php echo date('Y',
strtotime($publications['publication_year'])); ?></center></h1>  
              <div class="post-content">

                  <?php echo $publications['title']; ?> <br>
                  <ul><l1> <?php echo $publications['description']; ?></li> </ul>
                  <?php if(!empty($publications['image'])): ?>
                  <ul><l1><a class="link-download" href="<?php echo BASE_URL . "/assets/images/". $publications['image'] ?>"><?php echo $publications['image'] ?></a> </li> </ul>
                 
          
               <?php endif; ?> 

                
              </div>
          </div>
          </div>    
        <?php endforeach; ?>
      </div>
Posted
Updated 9-Dec-21 22:54pm
Comments
Nct Zen 15-Dec-21 11:08am    
Can you show me where I should put the code that you write? I'm tried but still no success.

1 solution

You need to move the display of the date field outside of the foreach loop. Alternatively check if the same date has already been displayed before displaying each item.
So start with an empty date field:
PHP
$lastdate = "";
foreach ...
    $year = strtotime($publications['publication_year']));
    if ($year != $lastdate) {
        $lastdate = $year;
    // it's a different year so display it
    }
    // display the individual publications
} // end of foreach
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900