Solved

Date Loop Inside Loop

Posted on 2010-11-23
11
218 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
  • 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
 
LVL 108

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 108

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:
patsmitty earned 500 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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

759 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now