looping in a loop?
9 posts by 3 authors in: Forums > CMS Builder
Last Post: January 12, 2012 (RSS)
By petejdg - October 12, 2011
<?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>
Re: [petejdg] looping in a loop?
Here's some code that I used to list types of exhibitions and the year that they occurred in for a resume list page.
The 'category' field is a pull down list field that get it's values from a multi-record database (advanced option) called 'resume_categories' with one field called 'category'. Take the field option values from the record number (num) and the field option labels from the 'category' field.
:label is pseudo field code to display the text in the category field and not the record number.
You can see the result here
Hope it gives you some ideas.
Best,
Jerry Kornbluth
<table align="left" border="0" cellpadding="0">
<?php $old_group = ''; // init blank var.
$old_year = ''; // init blank var.
foreach ($resumeRecords as $record): ?>
<?php $category = $record['category:label']; ?>
<?php $group = strtoupper($record['category']); // load sub-group value from record. ?>
<?php $year = $record['year']; // load year value from record. ?>
<tr>
<td align="left" >
<?PHP if ($group != $old_group) {echo "<div class='subheading' ><br />$category</div><br />";}?>
<?php
if (!preg_match("/^http:\/\//i", $record['url'])) {
$record['url'] = "http://" . $record['url']; }
?>
<table width="100%" cellpadding="1" border="0">
<tr>
<td class="text" width="9%" align="left" valign="top"><?php if ($year != $old_year) {echo "$year";}?></td>
<td class="text" width="2% "align="right"valign="top">• </td>
<td class=" text" align="left" valign="top"> <?php $entry = $record['entry']; ?> <?php echo $entry; ?><br />
<a class="special" href="<?php echo $record['url'] ?>" target="_blank"><span class="text"><u><?php echo $record['link_text'] ?></u></span></a>
</td>
</tr>
</table>
</td>
</tr>
<?PHP $old_group = $group; // retain sub-group name before moving to new record. ?>
<?PHP $old_year = $year; // retain sub-group name before moving to new record. ?>
<?php endforeach; ?>
</table> </td></tr>
</table>
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] looping in a loop?
By petejdg - October 12, 2011
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; ?>
Re: [petejdg] looping in a loop?
By Jason - October 13, 2011
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
Re: [Jason] looping in a loop?
By petejdg - January 12, 2012
http://wilsontrailer.com/trade_shows/index.php
Re: [petejdg] looping in a loop?
By Jason - January 12, 2012
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
Re: [petejdg] looping in a loop?
By Jason - January 12, 2012
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/