?
Solved

Date Loop Inside Loop

Posted on 2010-11-23
11
Medium Priority
?
228 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:katlees
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 34197867
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
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 34198056
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
 

Author Comment

by:katlees
ID: 34198115
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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34198373
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
 

Author Comment

by:katlees
ID: 34201101
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34201425
You might be able to use GROUP BY in the query.  Sorry I have been mostly swamped today.
0
 

Author Comment

by:katlees
ID: 34209693
Ray, if I use the Group By, it only shows one event per day. I need to show each event on each day.
0
 
LVL 11

Accepted Solution

by:
Bruce Smith earned 2000 total points
ID: 34210665
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34212641
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
 

Author Comment

by:katlees
ID: 34223032
patsmitty - it works good but it is only showing one month... December. It won't then show Jan & february.
0
 

Author Comment

by:katlees
ID: 34223034
I got it. I changed it to interval 3 month and it worked. thanks..
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question