Date Loop Inside Loop

I have the following code and need some help to manipulate it.
Right now it displays
Nov 2010
Saturday 27th
Event Name
Event Name

Saturday 4th
Event Name
Event Name

Monday 6th
Event Name

Tuesday 7th
Event Name
Event Name

Three things..
I need it to display the full month name so instead of Nov 10 it would show November 2010

When November is over, it would show December 2010, then January 2011 when January dates start

Each Month would show a new column - only shows the current month and next two months
So final version would be

November 2010                            December 2010                                January 2011
Saturday 27th                               Saturday 4th                                     Saturday 2nd
Event Name                                   Event Name                                       Event Name
Event Name
                                                       Monday 6th                                     Monday 4th
Monday 29th                                 Event                                                Event
Event
<?php
        $currentdate = date('Y-m-d');
 
        $event_query = "SELECT * FROM php_event_events WHERE dt >= NOW() AND dt <= DATE_ADD(NOW(), INTERVAL 1 MONTH) ORDER BY Dt;";
        $event_query_result = mysql_query($event_query);
        
        $TitleDate = "";
        $first_time = 1;
        
        if (mysql_num_rows($event_query_result) == 0)
        {
                echo "No current events found\n";
        }
        else
        {
                while ($event_row = mysql_fetch_assoc($event_query_result))
                {
                        
						$event = $event_row['title'];
			            $description = $event_row['description'];
						$event_time = $event_row['startTime'];
						$event_dates = $event_row['dt'];
						$display_date = date("M j, Y", strtotime($event_dates ));        
                        $EventMonth = date("M", strtotime($event_dates));
                        $EventYear = date("Y", strtotime($event_dates));
                        $EventDayOfMonth = date("jS", strtotime($event_dates));
                        $EventDayOfWeek = date("l", strtotime($event_dates));                  
                        
                        if ($TitleDate != $event_dates)
                        {
                                if (!$first_time)
                                {
                                   echo "<p></p>\n";
                                }
                                else
                                {
                                        $first_time = 0;
                                }
                                
                                echo "<p class=\"style2\">";
                                if ($TitleDate == "" or date("M Y",strtotime($TitleDate)) != date("M Y",strtotime($TitleDate)))
                                echo date("M Y",strtotime($event_dates))."<br />\n";
                                echo date("l jS",strtotime($event_dates)). "</p>\n";
                               
                                $TitleDate = $event_dates;
                                
                        }
                  

                        echo "$event\n";
     
                }
 
        }
?>

Open in new window

katleesAsked:
Who is Participating?
 
Bruce SmithConnect With a Mentor Software Engineer IICommented:
Remove the GROUP BY and add a $dateChecker variable and an IF statement to accomplish the following inside your loop:

November 2010
Date
Event
Event

Date
Event
Event

December 2010
Date
Event

Date
Event
Event

Here is the modified code and I think will help you out here. See the commenting in the code.

Cheers

 
$currentdate = date('Y-m-d');
$event_query = "SELECT * FROM php_event_events WHERE dt >= NOW() AND dt <= DATE_ADD(NOW(), INTERVAL 1 MONTH) ORDER BY Dt;";
$event_query_result = mysql_query($event_query);

$testMonth = 0;
$dateChecker=""; //set to null - will use later for checking purposes

if (mysql_num_rows($event_query_result) > 0)
{
     while ($event_row = mysql_fetch_assoc($event_query_result))
      
	    {
                $event_id = $event_row['ID'];
                $event_name = $event_row['title'];
                $event_date = $event_row['dt'];
	        $event_time = mysql2timestamp($event_date);
                $event_display_date = date("D, F j, Y", $event_time);
                $event_description = $event_row['description'];
                $curDate = getdate($event_time);
                $curMonth = $curDate['mon'];
                $curYear = $curDate['year'];
                $curFullMonth = $curDate['month'];
				 
                if(! ($testMonth == $curMonth ))
                { 
			echo("<div class=\"calendar_col\">");
                        echo("<h1>$curFullMonth $curYear</h1>");
                        $testMonth = $curMonth;
                } 
				
if($dateChecker=='' || $dateChecker!=$event_display_date) { //this will only print the date if it is NOT the same as the previous event's date OR if it is the first run through your loop
  echo "<br> $event_display_date";
 }
  echo('\n'.$event_name);

 echo "</div>\n";
 $dateChecker = $event_display_date; //sets the checker variable to last row queried from the database so that on the next loop, the IF statement will be able to tell if that event is on a different date or is on the same as the previous event
        }

Open in new window

0
 
jrm213jrm213Commented:
1. to display the full month, use "F" instead of "M"
2. to switch up months, start the script by getting the first records month, then compare it each time in the loop, if it changes, echo out the end of the container for that month, and then echo the start of a new container with the new month name in it...
0
 
jrm213jrm213Commented:
give the following a try, I added a div with the class column to break the data into columns, style as needed
<?php
        $currentdate = date('Y-m-d');
 
        $event_query = "SELECT * FROM php_event_events WHERE dt >= NOW() AND dt <= DATE_ADD(NOW(), INTERVAL 1 MONTH) ORDER BY Dt;";
        $event_query_result = mysql_query($event_query);
        
        $TitleDate = "";
        $curmonth = -1;
        $eventcount = 0;
        
        if (mysql_num_rows($event_query_result) == 0)
        {
                echo "No current events found\n";
        }
        else
        {
                while ($event_row = mysql_fetch_assoc($event_query_result))
                {
                        
						$event = $event_row['title'];
			            $description = $event_row['description'];
						$event_time = $event_row['startTime'];
						$event_dates = $event_row['dt'];
						$display_date = date("M j, Y", strtotime($event_dates ));        
                        $EventMonth = date("F", strtotime($event_dates));
                        $EventYear = date("Y", strtotime($event_dates));
                        $EventDayOfMonth = date("jS", strtotime($event_dates));
                        $EventDayOfWeek = date("l", strtotime($event_dates));                  
                        
                        if ($curmonth != date("M",$event_dates))
                        {            
                                if($curmonth > -1)
                                {
                                   echo "</div>";
                                }                  
                                echo "<div class="column"><p class=\"style2\">";
                                echo date("F Y",strtotime($event_dates))."<br />\n";
                                echo date("l jS",strtotime($event_dates)). "</p>\n\n";
                                $curmonth = date("M",$event_dates);      
                        }
                        echo "$event\n";
                        $eventcount++;
                }
            if($eventcount>0)
            {
              echo("</div>");
            }
        }
?>

Open in new window

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
katleesAuthor Commented:
I tried that code and added the backslashes in line 36 but it just gives me this:

November 2010
Saturday 27th

Saturday Surprise Lunch Lunch Saturday Surprise Lunch 12 Days of Christmas Saturday Surprise Lunch 12 Days of Christmas 12 Days of Christmas 12 Days of Christmas 12 Days of Christmas 12 Days of Christmas 12 Days of Christmas 12 Days of Christmas Saturday Surprise Lunch 12 Days of Christmas 12 Days of Christmas 12 Days of Christmas 12 Days of Christmas

So not showing December 2010 as the month for the new month and not showing date then event.
0
 
Ray PaseurCommented:
This article might be helpful.  Read it over.  I'll come back and look at the specifics of the question here in a little while.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
 
katleesAuthor Commented:
Thanks Ray.. Great article. I think I am having more issues with the loop. I have the code switched to the folllowing and am not sure how to do the second date loop. Right now I have it displaying
November 2010
Date - Event
Date - Event

December 2010
Date - Event
Date - Event

How do I loop the inside part so it looks like
November 2010
Date
Event
Event

Date
Event
Event

December 2010
Date
Event

Date
Event
Event
$currentdate = date('Y-m-d');
$event_query = "SELECT * FROM php_event_events WHERE dt >= NOW() AND dt <= DATE_ADD(NOW(), INTERVAL 1 MONTH) ORDER BY Dt;";
$event_query_result = mysql_query($event_query);

$testMonth = 0;


if (mysql_num_rows($event_query_result) > 0)
{

        while ($event_row = mysql_fetch_assoc($event_query_result))
      
	    {
                $event_id = $event_row['ID'];
                $event_name = $event_row['title'];
                $event_date = $event_row['dt'];
				$event_time = mysql2timestamp($event_date);
                $event_display_date = date("D, F j, Y", $event_time);
                $event_description = $event_row['description'];
                $curDate = getdate($event_time);
                $curMonth = $curDate['mon'];
                $curYear = $curDate['year'];
                $curFullMonth = $curDate['month'];
				 
                if(! ($testMonth == $curMonth ))
                { 
				echo("<div class=\"calendar_col\">");
                echo("<h1>$curFullMonth $curYear</h1>");
                     $testMonth = $curMonth;
                } 
				

  echo "<br> $event_display_date - $event_name \n";


 echo "</div>\n";               
        }

Open in new window

0
 
Ray PaseurCommented:
You might be able to use GROUP BY in the query.  Sorry I have been mostly swamped today.
0
 
katleesAuthor Commented:
Ray, if I use the Group By, it only shows one event per day. I need to show each event on each day.
0
 
Ray PaseurCommented:
I think patsmitty's solution will work for you, but I am wondering what is in this column after the query: $event_row['dt'] - is that a MySQL DATETIME field?  Just curious.
0
 
katleesAuthor Commented:
patsmitty - it works good but it is only showing one month... December. It won't then show Jan & february.
0
 
katleesAuthor Commented:
I got it. I changed it to interval 3 month and it worked. thanks..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.