
Jason
Staff
/ Moderator

Oct 13, 2011, 10:59 AM
Post #4 of 9
(773 views)
Shortcut
|
|
Re: [petejdg] looping in a loop?
[In reply to]
|
Can't Post
|
|
Hi, I took a look at your output and January and February are repeated twice. Here is another approach where we first organize all of our records into a multi-dimensional array, organized by year and then month, before we output them. We the output our data using 3 nested loops. Nested loops can be expensive, but the code is a little cleaner and may work a little better for you. Give this a try:
<?php header('Content-type: text/html; charset=utf-8'); ?> <?php /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */ // load viewer library $libraryPath = 'cmsAdmin/lib/viewer_functions.php'; $dirsToCheck = array('C:/inetpub/wwwroot/','','../','../../','../../../'); foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }} if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); } // load records list($trade_showsRecords, $trade_showsMetaData) = getRecords(array( 'tableName' => 'trade_shows', 'orderBy' => 'year', 'allowSearch' => false, )); $yearMonthToRecords = array(); foreach ($trade_showsRecords as $record) { $year = trim($record['year']); $month = trim($record['month']); // initialize array if (!array_key_exists($year, $yearMonthToRecords)) { $yearMonthToRecords[$year] = array(); } if (!array_key_exists($month, $yearMonthToRecords[$year])) { $yearMonthToRecords[$year][$month] = array(); } $yearMonthToRecords[$year][$month][] = $record; } ?> ------------------------------------------ <?php foreach ($yearMonthToRecords as $year => $monthsToRecords): ?> <div class='subheading'><br /><?php echo $year; ?></div><br /> <?php foreach ($monthsToRecords as $month => $records): ?> <div class='subheading2'><br /><?php echo $month; ?></div> <table width="720" cellpadding="0" border="0"> <?php foreach ($records as $record): ?> <tr> <td width="270" align="left" valign="top"><?php if ($record['link']): ?><a href="<?php echo $record['link'] ?>"target="_blank"> <?php echo $record['title'] ?></a><?php else: ?><?php echo $record['title'] ?><?php endif ?> </td> <td width="230" align="left" valign="top"><?php echo $record['location'] ?></td> <td width="180" align="left" valign="top"><?php echo $record['date'] ?></td> </tr> <?php endforeach ?> </table> <?php endforeach ?> <?php endforeach; ?> Hope this helps --------------------------------------------------- Jason Sauchuk - Programmer interactivetools.com Hire me! Save time by getting our experts to help with your project. http://www.interactivetools.com/consulting/
|