Solved

PHP Loop Inside Loop

Posted on 2008-09-30
11
200 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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
 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
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.

813 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