• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

PHP Loop Inside Loop

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
katlees
Asked:
katlees
  • 6
  • 5
1 Solution
 
Michael701Commented:
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
 
katleesAuthor Commented:
I am not sure what to change and where. You wrote up that code, but what do I replace?
0
 
Michael701Commented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
katleesAuthor Commented:
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
 
Michael701Commented:
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
 
katleesAuthor Commented:
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
 
katleesAuthor Commented:
The date and day of week can be removed from the event line
0
 
Michael701Commented:
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
 
Michael701Commented:
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
 
katleesAuthor Commented:
I would prefer that they are in columns so they line up...
0
 
katleesAuthor Commented:
Thanks.I got it I thnk. Will take some tweaking of the table cells, but it is good.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now