Solved

PHP Loop Inside Loop

Posted on 2008-09-30
11
198 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:katlees
Comment Utility
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
Comment Utility
The date and day of week can be removed from the event line
0
 
LVL 19

Expert Comment

by:Michael701
Comment Utility
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
Comment Utility
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
Comment Utility
I would prefer that they are in columns so they line up...
0
 

Author Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

763 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

12 Experts available now in Live!

Get 1:1 Help Now