looping in a loop?

9 posts by 3 authors in: Forums > CMS Builder
Last Post: January 12, 2012   (RSS)

By petejdg - October 12, 2011

I have set up a tradeshow administration with fields for: Year (drop down), Month (drop down), Title, Location and date(textfield). I found some code on this forum to do a group by year so I can show events by year and then by month and loop through each record for the month. I have the years grouping with results but now need to group by month and loop through that specific month's records. Total newbie here so thanks for any help.

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// 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',
));

?>

------------------------------------------------

<?php
$old_group = ''; // init blank var.
foreach ($trade_showsRecords as $record):
$group = $record['year']; // load sub-group value from record.
if ($group != $old_group) { // If different from the last sub-group value, print the sub-group name.
echo "<h5>$group</h5>";
}?>
<table width="740" border="1" cellspacing="0" cellpadding="2">
<tr>
<td colspan="3"><?php echo $record['month'] ?></td>
</tr>
<tr>
<td width="281"><a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a></td>
<td width="228"><?php echo $record['location'] ?></td>
<td width="219"><?php echo $record['date'] ?></td>
</tr>
<?php $old_group = $group; // retain sub-group name before moving to new record. ?>
<?php endforeach ?>
</table>
Attachments:

wt-tradeshowoutput.gif 11K

Re: [gkornbluth] looping in a loop?

By petejdg - October 12, 2011

Thanks for the help. I really don't exactly know what I am doing but played around with it tonight and thought I had it working but for some reason it looks like it is limiting two longer months to just 10 items?????

My code now:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// 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',
));

?>
------------------------------------------
<?php $old_group = ''; // init blank var.
$old_month = ''; // init blank var.
foreach ($trade_showsRecords as $record): ?>
<?php $group = $record['year']; // load sub-group value from record. ?>
<?php $month = $record['month']; // load year value from record. ?>

<?php if ($group != $old_group) {echo "<div class='subheading'><br />$group</div><br />";}?>


<?php if ($month != $old_month) {echo "<div class='subheading2'><br />$month</div>";}?>
<table width="720" cellpadding="0" border="0">
<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>
</table>

<?PHP $old_group = $group; // retain sub-group name before moving to new record. ?>
<?PHP $old_month = $month; // retain sub-group name before moving to new record. ?>
<?php endforeach; ?>
Attachments:

wt-tradeshow.gif 50K

Re: [petejdg] looping in a loop?

By Jason - October 13, 2011

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


// 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 - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] looping in a loop?

By petejdg - October 13, 2011

Wow. Brilliant! Works awesome... thank you!

Re: [Jason] looping in a loop?

By petejdg - January 12, 2012

Jason, this is working good, but how can I have the records sorted by the drag sort order? Since some of the tradeshows span multiple days, etc. I need to be able to choose the order they appear for the day. I don't see how it is ordering them currently but would like to do the drag sort order.
http://wilsontrailer.com/trade_shows/index.php

Re: [petejdg] looping in a loop?

By Jason - January 12, 2012

Hi,

Currently, your records are being sorted by year, this is to ensure that the years are output in a reasonable order (ie, 2011 before 2012). If you want to revert to dragSortOrder you can try removing the orderBy option altogether:

// load records
list($trade_showsRecords, $trade_showsMetaData) = getRecords(array(
'tableName' => 'trade_shows',
'orderBy' => 'year',
'allowSearch' => false,
));


How well this will work will depend on the order of all your records since the code is re-arranging the records by year/month.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] looping in a loop?

By petejdg - January 12, 2012

Is there a way leave it sorty by year and month but then allow for a drag sort order for just the tradeshow records? Or is my only option to just delete the sort by year like you stated?

Re: [petejdg] looping in a loop?

By Jason - January 12, 2012

Hi,

One option would be to sort your records as they are being outputted. Take a look at this post:

http://www.interactivetools.com/forum/gforum.cgi?post=88597#88597

If you put the function subval_sort() on your page, you should be able to use it like this:

<?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 $records = subval_sort($records,'dragSortOrder', 'DESC'); ?>
<?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 - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/