Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP Loop Inside Loop

Posted on 2008-09-30
11
Medium Priority
?
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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.
Suggested Courses

610 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