Link to home
Start Free TrialLog in
Avatar of katlees
katleesFlag for United States of America

asked on

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

Avatar of jrm213jrm213
jrm213jrm213
Flag of United States of America image

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...
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

Avatar of katlees

ASKER

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.
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.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
Avatar of katlees

ASKER

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

You might be able to use GROUP BY in the query.  Sorry I have been mostly swamped today.
Avatar of katlees

ASKER

Ray, if I use the Group By, it only shows one event per day. I need to show each event on each day.
ASKER CERTIFIED SOLUTION
Avatar of Bruce Smith
Bruce Smith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of katlees

ASKER

patsmitty - it works good but it is only showing one month... December. It won't then show Jan & february.
Avatar of katlees

ASKER

I got it. I changed it to interval 3 month and it worked. thanks..