Solved

PHP Loop Inside Loop

Posted on 2008-09-30
11
199 Views
Last Modified: 2012-05-05
I am very new to PHP and slowly learning. I had someone help me with the code below but need a tweak on the formatting side.

Currently it displays like this:

October 2008
   Event
   Date, Day of week, time, Location, tickets

   line

   Event
   Date, Day of week, time, Location, tickets

I would like it to change to display like this
October 2008
Day of week, Date (Friday, 3rd)
   Event - Time - Location - Tickets
   Line
   Event - Time - Location - Tickets
   Line
Day of week, Date (Saturday, 4th)
 Event - Time - Location - Tickets
   Line
   Event - Time - Location - Tickets
   Line

<?php

	$currentdate = date('Y-m-d');
 

	$event_query = "SELECT * FROM Events WHERE Date >= NOW() AND Date <= DATE_ADD(NOW(), INTERVAL 1 MONTH) ORDER BY Date LIMIT 15;";

	$event_query_result = mysql_query($event_query);

	

	$CurrentMonth = "";

	$CurrentYear = "";

	$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_name = $event_row['Name'];         

			$event_dates = $event_row['Date'];    

			$display_date = date("M j, Y", strtotime($event_dates ));        

			$event_time = $event_row['DisplayTime'];    

			$event_tickets = $event_row['Tickets'];        

			$event_id = $event_row['ID'];

			$event_location = $event_row['Location'];

			$eventCode = $event_row['EventCode'];

			$seasonCode = $event_row['SeasonCode'];

			$OnSale = $event_row['OnSale'];

			$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 ($EventMonth != $CurrentMonth || $EventYear != $CurrentYear)

			{

				if (!$first_time)

				{

					echo "</tr>\n";

					echo "</table>\n";

					echo "<p class=\"style18\"></p>\n";

				}

				else

				{

					$first_time = 0;

				}

				

				echo "<p class=\"style2\">$EventMonth $EventYear</p>\n";

				echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\">\n";

				

				$CurrentMonth = $EventMonth;

				$CurrentYear = $EventYear;

			}

			else

			{

				echo "</tr>\n";

				echo "<tr>\n";

			

				echo "<td colspan=\"5\" class=\"style3\"><div align=\"center\"><img src=\"images/line.jpg\" alt=\"line\" width=\"500\" height=\"5\"></div></td>\n";

				echo "</tr>\n";

				echo "</table>\n";

				echo "<table width=\"100%\">\n";

			}
 

			echo "<tr>\n";

			echo "<td colspan=\"2\" class=\"style18\"><a href=\"display_event.php?id=$event_id\">$event_name</a></td>\n";

            echo "<td width=\"22%\">&nbsp;</td>\n";

            echo "<td width=\"11%\">&nbsp;</td>\n";

            echo "<td width=\"28%\">&nbsp;</td>\n";

			echo "</tr>\n";

			echo "<tr>\n";

            echo "<td width=\"15%\" class=\"style3\">$EventDayOfMonth</td>\n";

            echo "<td width=\"24%\"><span class=\"style3\">$EventDayOfWeek</span></td>\n";

            echo "<td><span class=\"style3\">$event_location</span></td>\n";

            echo "<td><span class=\"style3\">$event_time</span></td>\n";

            echo "<td><span class=\"style3\" align=\"right\">";

			

			if ($event_tickets)

			{

				if ($currentdate >= $OnSale)

				{

					echo "<a href=\"http://ev12.evenue.net/cgi-bin/ncommerce3/SEGetEventInfo?ticketCode=GS%3ARUSHMORE%3A$seasonCode%3A$eventCode%3A&linkID=rushmore&shopperContext=&caller=&appCode=\">Buy Tickets</a>";

				}

				else

				{

					$OnSaleDate = date("n/j/Y", strtotime($OnSale));

					echo "tickets on sale $OnSaleDate";

				}

			}

			else

			{

				echo "&nbsp;";

			}

				

			echo "</span></td>\n";

		}

		

		echo "</tr>\n";

		echo "</table>\n";

	}

?>

Open in new window

0
Comment
Question by:katlees
  • 6
  • 5
11 Comments
 
LVL 19

Expert Comment

by:Michael701
ID: 22608658
Here's some notes to clean up your code.

You don't need to
$event_name = $event_row['Name'];        
$event_dates = $event_row['Date'];
for all the fields. Just change the
$event_row = mysql_fetch_assoc...
to
$event = mysql_fetch_assoc...
then you can reference the fields as $event['date']

next convert this
echo "<td colspan=\"2\" class=\"style18\"><a href=\"display_event.php?id=$event_id\">$event_name</a></td>\n";
            echo "<td width=\"22%\"> </td>\n";
to
echo "<td colspan=2 class='style18'><a href='display_event.php?id=".$event['ID']."'>".$event['Name']."</a></td>\n";
            echo "<td width='22%'> </td>\n";
Note, html will accept single quotes, this cleans up your php. Also html does not require quotes around numberic values like colspan. It does require quotes on width in percentages but not in pixels

Hope this isn't too confusing.

Enjoy


//outside the loop

        $TitleDate = "";
 
 
 

                while ($event = mysql_fetch_assoc($event_query_result)) // Just call it $event

                {

                        

                        if ($TitleDate != $event['date']) // check to see if the date has changed

                        {

                                if ($TitleDate != "" ) // use this instead of $first_time

                                {

                                        echo "</tr>\n";

                                        echo "</table>\n";

                                        echo "<p class=\"style18\"></p>\n";

                                }

                                

                                echo "<p class=\"style2\">".date("l, M jS",strtotime($event['date'])) ."</p>\n";

                                echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\">\n";

                                

                                $TitleDate = $event['date'];

Open in new window

0
 

Author Comment

by:katlees
ID: 22608709
I am not sure what to change and where. You wrote up that code, but what do I replace?
0
 
LVL 19

Expert Comment

by:Michael701
ID: 22611577
Ok, sorry I probably was throwing too much at you at once.

Let's start with this code. it sets up $TitleDate and will display every date change.
<?php

        $currentdate = date('Y-m-d');

 

        $event_query = "SELECT * FROM Events WHERE Date >= NOW() AND Date <= DATE_ADD(NOW(), INTERVAL 1 MONTH) ORDER BY Date LIMIT 15;";

        $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_name = $event_row['Name'];         

                        $event_dates = $event_row['Date'];    

                        $display_date = date("M j, Y", strtotime($event_dates ));        

                        $event_time = $event_row['DisplayTime'];    

                        $event_tickets = $event_row['Tickets'];        

                        $event_id = $event_row['ID'];

                        $event_location = $event_row['Location'];

                        $eventCode = $event_row['EventCode'];

                        $seasonCode = $event_row['SeasonCode'];

                        $OnSale = $event_row['OnSale'];

                        $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 "</tr>\n";

                                        echo "</table>\n";

                                        echo "<p class=\"style18\"></p>\n";

                                }

                                else

                                {

                                        $first_time = 0;

                                }

                                

                                echo "<p class=\"style2\">".date("l, M jS",strtotime($event_dates)). "</p>\n";

                                echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\">\n";
 

                                $TitleDate = $event_dates;

                                

                        }

                        else

                        {

                                echo "</tr>\n";

                                echo "<tr>\n";

                        

                                echo "<td colspan=\"5\" class=\"style3\"><div align=\"center\"><img src=\"images/line.jpg\" alt=\"line\" width=\"500\" height=\"5\"></div></td>\n";

                                echo "</tr>\n";

                                echo "</table>\n";

                                echo "<table width=\"100%\">\n";

                        }

 

                        echo "<tr>\n";

                        echo "<td colspan=\"2\" class=\"style18\"><a href=\"display_event.php?id=$event_id\">$event_name</a></td>\n";

            echo "<td width=\"22%\"> </td>\n";

            echo "<td width=\"11%\"> </td>\n";

            echo "<td width=\"28%\"> </td>\n";

                        echo "</tr>\n";

                        echo "<tr>\n";

            echo "<td width=\"15%\" class=\"style3\">$EventDayOfMonth</td>\n";

            echo "<td width=\"24%\"><span class=\"style3\">$EventDayOfWeek</span></td>\n";

            echo "<td><span class=\"style3\">$event_location</span></td>\n";

            echo "<td><span class=\"style3\">$event_time</span></td>\n";

            echo "<td><span class=\"style3\" align=\"right\">";

                        

                        if ($event_tickets)

                        {

                                if ($currentdate >= $OnSale)

                                {

                                        echo "<a href=\"http://ev12.evenue.net/cgi-bin/ncommerce3/SEGetEventInfo?ticketCode=GS%3ARUSHMORE%3A$seasonCode%3A$eventCode%3A&linkID=rushmore&shopperContext=&caller=&appCode=\">Buy Tickets</a>";

                                }

                                else

                                {

                                        $OnSaleDate = date("n/j/Y", strtotime($OnSale));

                                        echo "tickets on sale $OnSaleDate";

                                }

                        }

                        else

                        {

                                echo " ";

                        }

                                

                        echo "</span></td>\n";

                }

                

                echo "</tr>\n";

                echo "</table>\n";

        }

?>

Open in new window

0
 

Author Comment

by:katlees
ID: 22611586
Close, but i want it to display like this

Sept. 08
Friday 3rd
Event Name, Time, Place, Location
Event Name, Time, Place, Location
Saturday 4th
Event Name, Time, Place, Location
Event Name, Time, Place, Location

Oct 08
Thursday 2nd
Event Name, Time, Place, Location
Event Name, Time, Place, Location
Friday 3rd
Event Name, Time, Place, Location
Event Name, Time, Place, Location
0
 
LVL 19

Accepted Solution

by:
Michael701 earned 500 total points
ID: 22611621
oh, I see.
<?php

        $currentdate = date('Y-m-d');

 

        $event_query = "SELECT * FROM Events WHERE Date >= NOW() AND Date <= DATE_ADD(NOW(), INTERVAL 1 MONTH) ORDER BY Date LIMIT 15;";

        $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_name = $event_row['Name'];         

                        $event_dates = $event_row['Date'];    

                        $display_date = date("M j, Y", strtotime($event_dates ));        

                        $event_time = $event_row['DisplayTime'];    

                        $event_tickets = $event_row['Tickets'];        

                        $event_id = $event_row['ID'];

                        $event_location = $event_row['Location'];

                        $eventCode = $event_row['EventCode'];

                        $seasonCode = $event_row['SeasonCode'];

                        $OnSale = $event_row['OnSale'];

                        $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 "</tr>\n";

                                        echo "</table>\n";

                                        echo "<p class=\"style18\"></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";

                                echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\">\n";
 

                                $TitleDate = $event_dates;

                                

                        }

                        else

                        {

                                echo "</tr>\n";

                                echo "<tr>\n";

                        

                                echo "<td colspan=\"5\" class=\"style3\"><div align=\"center\"><img src=\"images/line.jpg\" alt=\"line\" width=\"500\" height=\"5\"></div></td>\n";

                                echo "</tr>\n";

                                echo "</table>\n";

                                echo "<table width=\"100%\">\n";

                        }

 

                        echo "<tr>\n";

                        echo "<td colspan=\"2\" class=\"style18\"><a href=\"display_event.php?id=$event_id\">$event_name</a></td>\n";

            echo "<td width=\"22%\"> </td>\n";

            echo "<td width=\"11%\"> </td>\n";

            echo "<td width=\"28%\"> </td>\n";

                        echo "</tr>\n";

                        echo "<tr>\n";

            echo "<td width=\"15%\" class=\"style3\">$EventDayOfMonth</td>\n";

            echo "<td width=\"24%\"><span class=\"style3\">$EventDayOfWeek</span></td>\n";

            echo "<td><span class=\"style3\">$event_location</span></td>\n";

            echo "<td><span class=\"style3\">$event_time</span></td>\n";

            echo "<td><span class=\"style3\" align=\"right\">";

                        

                        if ($event_tickets)

                        {

                                if ($currentdate >= $OnSale)

                                {

                                        echo "<a href=\"http://ev12.evenue.net/cgi-bin/ncommerce3/SEGetEventInfo?ticketCode=GS%3ARUSHMORE%3A$seasonCode%3A$eventCode%3A&linkID=rushmore&shopperContext=&caller=&appCode=\">Buy Tickets</a>";

                                }

                                else

                                {

                                        $OnSaleDate = date("n/j/Y", strtotime($OnSale));

                                        echo "tickets on sale $OnSaleDate";

                                }

                        }

                        else

                        {

                                echo " ";

                        }

                                

                        echo "</span></td>\n";

                }

                

                echo "</tr>\n";

                echo "</table>\n";

        }

?>

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:katlees
ID: 22611637
Close....

You have Oct. 08
Thursday 2nd
Event Name
Location Time
Event Name
Location Time

I need Event name and location and time in the same row.
0
 

Author Comment

by:katlees
ID: 22611639
The date and day of week can be removed from the event line
0
 
LVL 19

Expert Comment

by:Michael701
ID: 22611661
I think you're referring to these lines of code

            echo "<td width=\"15%\" class=\"style3\">$EventDayOfMonth</td>\n";
            echo "<td width=\"24%\"><span class=\"style3\">$EventDayOfWeek</span></td>\n";

you could just change them to

            echo "<td width=\"15%\">&nbsp;</td>\n";
            echo "<td width=\"24%\">&nbsp;</td>\n";

If you were to remove these lines you'd have to recalculate the widths of the remaining columns to have them add up to 100% and also look for the colspan= lines and adjust them for 2 less columns.
0
 
LVL 19

Expert Comment

by:Michael701
ID: 22611685
re: "I need Event name and location and time in the same row."

Do you still want things aligned in table columns? Or, just one line of text per event?

ie

Oct. 08
Thursday 2nd
Event Name Location Time Tickets
Event Name Location Time Tickets

Oct. 09
Friday 3rd
Event Name Location Time Tickets
0
 

Author Comment

by:katlees
ID: 22611693
I would prefer that they are in columns so they line up...
0
 

Author Comment

by:katlees
ID: 22611700
Thanks.I got it I thnk. Will take some tweaking of the table cells, but it is good.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Passing variables to stored procedure 3 34
nagios 4 php error after installation 6 69
Php pie charts 3 26
WordPress TK Title 8 23
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

947 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

21 Experts available now in Live!

Get 1:1 Help Now