GROUPING SUBTOPICS IN YOUR LIST VIEW - Aug 2nd, 2010


InHouse offered this simple way of grouping the contents of List Views. I thought it might be useful for someone. Share
and enjoy.

In the Editor, create a field to hold the group names. Use the “List” option with either a pull down or check box
format to limit user mistakes and make this field mandatory as well. In this example the field is called "type". There
are also fields called “title” and “content”.



<?PHP
$old_group = ''; // init blank var.
foreach ($yourtableRecord as $record):
$group = $record['type']; // 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>";
}
?>

<a href="<?PHP echo $record['_link'] ?>"><?PHP echo $record['title'] ?></a>
<?PHP echo $record['content'] ?>

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



If you want to only show a single sub group then use an IF statement within the FOREACH loop:



<?PHP
$desired_group = 'yourgroupname'; // init target var.
foreach ($yourtableRecord as $record):

if (
$record['type'] == $desired_group ) { // Examine the current Type and see if it matches the desired group type.
?>

<a href="<?PHP echo $record['_link']; ?>"><?PHP echo $record['title']; ?></a>
<?PHP echo $record['content'] ?>

}// End If ?>
<?PHP endforeach ?>


Thanks InHouse!!!

Here’s how I used this idea on the CMSBuilder Cookbook Links page with a table called “resources” and fields
called “category”, url, and link_text”. The line



<table width="800" border="0" cellpadding="0">

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

<tr>
<td align="left" class="Medium-Text">
<?PHP if ($group != $old_group) {echo "<h2>$group</h2>";}?>

<a href="<?php echo $record['url'] ?>" target="_blank"><?php echo $record['link_text'] ?></a>
<br /><?php echo $record['content'] ?><hr align="center" color="#999999" /></td>
</tr>

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

</table>



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service