There is an awesome tag in Textpattern called if_different. I use it all the time to sort loops of entries (or any looped output) into grouped headers.
It basically gets the previous variable it contained and output from the loop and compares it to current variable it’s about to output, if the value is different will it output the contents.
The best part is you don’t need to rely on developers to add header tags to their plugins as it naturally worked in any looping data tags.
Examples…
{exp:weblog:entries weblog="blog" status="open" limit="999" dynamic="off"}
{exp:if_different}<h1>{custom_field}</h1>{/exp:if_different}
<a href="http://{url_title_path=/blog/}">{title}</a>
{/exp:weblog:entries}Bikes * Bike Type 1 * Bike Type 2 * Bike Type 3 * Bike Type 4 Cars * Car Type 1 * Car Type 2
{exp:custom_module}
{exp:if_different}<h1>{custom_date format="%Y"}</h1>{/exp:if_different}
{exp:if_different}<h2>{custom_date format="%M"}</h2>{/exp:if_different}
{custom_date format="%d %h"} - {title}
{/exp:custom_module}2009 January day hour - Event 1 day hour - Event 5 Febuary day hour - Event 4
I relies date and category heading tags can sort of achieve this but what if you want to group by another value or group in a tag that doesn’t support those methods?
You can certainly do something like that with a few lines of PHP:
<?php $prev = ""; $curr = ""; ?>
{exp:weblog:entries ...}
<?php $prev = $curr; // store the previous value
$curr = "{my_custom_field}"; // assign current value
if ($curr != $prev) { // now compare: if current value == previous value, not different
... // Yes, it's different!
}?>
{/exp:weblog:entries}Makes sense?
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.