The following line in the code needs some work:
echo "<TR><TD WIDTH=\"740\" HEIGHT=\"5\" COLSPAN=\"5\" CLASS=\"boldText\">$event_month</TD></TR>";
Perhaps it should be something more along the lines of:
if($cur_month != $old_month)
{
echo "<TR><TD WIDTH=\"740\" HEIGHT=\"5\" COLSPAN=\"5\" CLASS=\"boldText\">$event_month</TD></TR>";
// Let's update the $old_month var
$old_month = $cur_month;
}
But where do $cur_month & $old_month come into play? Well, you'd have to add it to your loop. First, you'd have to declare the $old_month as an empty string outside your loop (as a default). Then in the loop make $cur_month have the value of $from_month. Now, do the if() check I gave above.
Now, with those modifications you get something like the following:
$result = mysql_query ("SELECT diary_id, event_date, event, event_description, location, item_id, item_type, item_description from diary ORDER by event_date DESC, event ASC");
$numrows=mysql_num_rows($result);
echo "<TABLE CELLSPACING=\"0\" CELLPADDING=\"0\" BORDER=\"0\" WIDTH=\"740\">";
if ($numrows == 0)
{
echo "<TR><TD WIDTH=\"740\" HEIGHT=\"10\" COLSPAN=\"2\" CLASS=\"errText\">There are no results that match your query</TD></TR>\n";
}
if ($numrows > 0)
{
echo "<TR><TD WIDTH=\"740\" HEIGHT=\"10\" COLSPAN=\"2\" CLASS=\"mainText\">There are <SPAN CLASS=\"boldText\">$numrows</SPAN> entires in the diary database.<BR><BR></TD></TR>\n";
echo "<TR><TD WIDTH=\"80\"><P CLASS=\"boldText\">Event Date</P></TD>";
echo "<TD WIDTH=\"225\"><P CLASS=\"boldText\">Event</P></TD></TR>\n";
$old_month = ''; // Default for $old_month.
while ($myrow = @mysql_fetch_array($result))
{
$diary_id=$myrow["diary_id"];
$event_date=$myrow["event_date"];
$event=stripslashes($myrow["event"]);
$event_description=stripslashes($myrow["event_description"]);
$location=stripslashes($myrow["location"]);
$item_id=$myrow["item_id"];
$item_type=stripslashes($myrow["item_type"]);
$item_description=stripslashes($myrow["item_description"]);
//Explode the date to show months
$e_date = explode("-", "$event_date");
$from_year = $e_date[0];
$from_month = $cur_month = $e_date[1]; // Added the $cur_month attribute.
$from_day = $e_date[2];
$new_event_date = $from_day . "-" . $from_month . "-" . $from_year;
$todays_date = gmdate("Y-M-d");
if ($event_date < $todays_date)
$font_class="CLASS=\"errText\"";
else
$font_class="CLASS=\"mainText\"";
echo "<TR>";
if($cur_month != $old_month) // i.e. the month has changed
{
echo "<TD WIDTH=\"740\" HEIGHT=\"5\" COLSPAN=\"5\" CLASS=\"boldText\">$event_month</TD></TR>";
$old_month = $cur_month;
}
echo "<TR><TD WIDTH=\"80\" $font_class VALIGN=\"top\">$new_event_date</TD>";
echo "<TD WIDTH=\"225\" $font_class VALIGN=\"top\">$event<BR>$event_description</TD></TR>";
}
}
echo"</TABLE>";