Sub Grouping in List Views - One Way of Doing It.

14 posts by 6 authors in: Forums > CMS Builder
Last Post: March 12, 2010   (RSS)

By InHouse - April 16, 2008 - edited: April 17, 2008

Not really a question this time, but a suggestion for others. I've worked up this simple way of sub-grouping contents of List Views. I thought it might be useful for someone.
Share and enjoy.

In the Editor, create a field to hold the sub-group names. I used a List option to limit user mistakes. I called this the "type" field. Have the list sorted by that type field.

<h2>Grouped List View</h2>
<?php
$old_group = ''; // init blank var.
foreach ($listRows 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>";
}?>
<span class="archive_entry">
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a>
<?php echo $record['preview'] ?>
</span>
<?php $old_group = $group; // retain sub-group name before moving to new record. ?>
<?php endforeach ?>


Apologies for leaving in my own project-specific classes and HTML.

J.

Re: [InHouse] Sub Grouping in List Views - One Way of Doing It.

By Dave - April 17, 2008

Thanks for sharing your code! :)
Dave Edis - Senior Developer
interactivetools.com

Re: [studio-a] Sub Grouping in List Views - One Way of Doing It.

By InHouse - November 5, 2008 - edited: November 5, 2008

Hi studio-a.

Glad you found the snippet helpful.

As for showing one group only, I would think that you could either limit the group pulled from the database with the WHERE clause, or just use an IF statement within the FOREACH loop:


Conditional:

<h2>Grouped List View</h2>
<?php
$desired_group = 'desiredGroupNameString'; // init target var.
foreach ($listRows as $record):

if ($record['type'] == $desired_group ) { // Examine the current Type and see if it matches the desired group type.
?>
<span class="archive_entry">
<a href="<?php echo $record['_link']; ?>"><?php echo $record['title']; ?></a>
<?php echo $record['preview'] ?>
</span>
}// End If ?>
<?php endforeach ?>


I've used something similar in cases where I needed the entire list AND the single group element showing on the same page. One database call will manage both displays.

However, if you only need the one group to be shown on the page, I would think the efficient way would be to just limit what is drawn from the database using a WHERE filter on the SQL request.

Hope this helps,
J.

Re: [InHouse] Sub Grouping in List Views - One Way of Doing It.

By _kate_ - November 11, 2008 - edited: November 11, 2008

Excellent code!! Just what I was looking for! Thanks for sharing :)

I've tried to combine this with another code which should sort the products into columns of 5 but I'm not sure how. Using the below what happens is the page displays the list of item titles, then all of the products (not grouped under their titles). Can anyone let me know what I'm doing wrong? :( The items are being sorted into columns of 5 still however...


<?php
$old_group = ''; // init blank var.
foreach ($kfiveRecords as $record):
$group = $record['brand']; // load sub-group value from record.
if ($group != $old_group) { // If different from the last sub-group value, print the sub-group name.
echo "<strong>$group</strong><br /><br />";
}?>

<td width="200" align="center" valign="top" style="padding-bottom: 16px;">

<?php echo $record['title'] ?><br />

<?php foreach ($record['brochure'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>
<?php elseif ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" /><br/>
<?php else: ?>
<a href="<?php echo $upload['urlPath'] ?>" target="_blank" class="BrochureImg">
<?php foreach ($record['thumbnail'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" alt="" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" /><br/>
<?php elseif ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" alt="" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" vspace="6" /><br/>
<?php else: ?>
</a><br/>
<?php endif ?>
<?php endforeach ?>
<?php endif ?>
<?php endforeach ?>

<?php foreach ($record['brochure'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>
<?php elseif ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" /><br/>
<?php else: ?>
<a href="<?php echo $upload['urlPath'] ?>" target="_blank">View Brochure ></a><br/></td>

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


<?php $maxCols=5; if (@++$count1 % $maxCols == 0): ?></tr><tr><?php endif; ?>
<?php endforeach ?>


<?php if (!$kfiveRecords): ?>
There are currently no products. Please check this page again shortly.
<?php endif ?>

Re: [_kate_] Sub Grouping in List Views - One Way of Doing It.

By Dave - November 11, 2008

Hi Kate,

Can you post an url and/or attach the file?

Are you sorting by group first? That's a requirement of this method.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Sub Grouping in List Views - One Way of Doing It.

By _kate_ - November 12, 2008

Hi,
Here is the file. Not sure what you mean when you say sort by groups.

Thanks!
Attachments:

kfive.php 5K

Re: [_kate_] Sub Grouping in List Views - One Way of Doing It.

By Dave - November 12, 2008

In the Section Editor for this section under the "Sorting" tab, what does the "Order By" field say?

You're going to want to sort by brand first before any other fields.

It's hard to get a sense of what might be wrong from the file. The code looks good from everything I can see.

If you can email me the url I can take a look for you (dave@interactivetools.com). If you send FTP and CMS login details as well it will make it easier to fix. (Email, don't post login details to the forum).

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Sub Grouping in List Views - One Way of Doing It.

By rez - October 18, 2009 - edited: October 18, 2009

Hi,

I'm trying to use this code. I have an editor of categories (the groups). I use this to populate a list in another editor for the items. So it's the same setup, but the client can edit the categories.

The problem is that I am using the advanced option to populate the list from another editor in my items. I am using "num" for the value and "title" for the labels. Using the code in this thread, my categories in my view page are numbers. :(

How can I show the "labels, not the numbers? It works if I make both the value and categories the same (using "title" for both value and labels in the list of an editor)but if someone changes the title name, all the items in that category will be screwed up.

How can i use "num" for the value in a list populated from another database but show the "title" on my viewer pages? I can't figure out how else to make a category with items list on a viewer page. (food menu)

Re: [rez] Sub Grouping in List Views - One Way of Doing It.

By rez - October 20, 2009

I hope I'm clear. My "groups" are all from a different editor using a list. With "num" as value and "title" (group name) as the label.

All I can display in a viewer is a number as the group name. How do you display the "label" field?