# Interrupting a while loop

**URL:** <https://forum.kirupa.com/t/interrupting-a-while-loop/318734>\
**Category:** Uncategorized\
**Created:** [January 28, 2011, 3:44pm UTC](https://forum.kirupa.com/t/interrupting-a-while-loop/318734 "2011-01-28T15:44:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![munkiboi](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@munkiboi](https://forum.kirupa.com/u/munkiboi)\
**Post date:** [January 28, 2011, 3:44pm UTC](https://forum.kirupa.com/t/interrupting-a-while-loop/318734/1 "2011-01-28T15:44:07Z")

</div>

I have a while loop that lists all events in my MYSQL database by date. All working fine.

However, I would like to break the list by month so that a header is shown only once then list the events in that month, inserts a header at the first record of the next month an so on. My current code is inserting the header after each event. I understand why, but cannot seem to come up with a way of doing this.

Code as follows:

```php

while ($rows2 = mysql_fetch_assoc($result2)){
    
$next_title = $rows2['title'];
$long_string2 = $rows2['body'];
$next_link = $rows2['ID'];

$limit = 100;
if (strlen($long_string2) > $limit){
    $next_body = substr($long_string2, 0, strrpos(substr($long_string2, 0, $limit), ' ')) . '...';
    }else{
    $next_body = $long_string2;
    }

echo '<h2>'.$rows2['month_event'].'</h2>';

echo '<div id="calendar"><div id="date"><p class="date">'.$rows2['event_date'].'</p></div>
<div id="calendar_text">
<h3>'.$next_title.'</h3>
  <p class="news">'.nl2br($next_body).'</p>
  </div>
  <div id="date_link"><a href="events.php?id='.$rows2['ID'].'">read more>></a></div>  
  </div>';  

}

```

The header that defines the month is

echo ‘\<h2\>’.$rows2[‘month\_event’].’\</h2\>’;

Any help or pointers in the right direction greatly appreciated.

Thanks
