Sorting Based on Attribute Content

Author Message

Betsy Gamrat

Friday 26 January 2007 8:19:11 am

Hi,

If you need to sort content based on attributes, you can use this approach:

First, install the <b>sort template plugins</b>. I used them on eZ 3.8.6 and it was flawless (my thanks to the author).

http://ez.no/community/contribs/template_plugins/arraysortoperator

Once the extension is installed, you can use it like so:

{def $sorted_month_array=array()}
{foreach $months as $k => $v}
  {set $sorted_month_array=$sorted_month_array|append(concat($v.data_map.month.value.0,'-',$k))}
{/foreach}
{set $sorted_month_array=$sorted_month_array|sort('numeric')}

In this case, I created an array of the attribute value (<i>$v.data_map.month.value.0</i>), a dash (<i>-</i>) that serves as a separator, and the array index (<i>$k</i>). Then, the <b>sort</b> template operator was used, with the numeric option - to ensure 11 came after 2.

To access the array, you loop through the elements, exploding out the data from the strings and using it as an index into the array.

{* Loop through the sorted array and if the month is supposed to be displayed in the slot - display it *}

{def $bits=''}
{def $m=0}
{for 1 to 12 as $i}
  <td>
    {set $bits=$sorted_month_array[$m]|explode('-')}
    {if $bits.0|eq($i)}
      {node_view_gui content_node=$months[$bits.1] view='line' format='cell'}
      {set $m=$m|inc}
    {else}
      &nbsp;
    {/if}
  </td>
{/for}

In this case, the months were being displayed in a table, in a row of 12 cells. I wanted each month to go into the proper cell (January would be first, February second). The array did not necessarily have all 12 months - so the loop creates the 12 cells and decides whether to put the content in based on the content.

<b>$bits</b> is the cross-reference. The first number is the month, the second is the position in the array (fetched).

Hope this helps.

Betsy Gamrat

Monday 02 April 2007 8:11:01 pm

As often happens - another approach appears. This one builds a sorted list, rather than sorting it after assembly.

  {def $sorted_event_array=array() $value='' $i=0}
  {foreach $events as $k => $v}
    {set $value=concat($v.data_map.start_date.content.day,'-',$k)}
    {set $i=0}
    {while and(is_set($sorted_event_array[$i]),$value|gt($sorted_event_array[$i]))}
        {set $i=$i|inc}
    {/while}
    {set $sorted_event_array=$sorted_event_array|insert($i,$value)}
  {/foreach}

:)

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 19 2025 00:27:09
Script start
Timing: Jan 19 2025 00:27:09
Module start 'layout'
Timing: Jan 19 2025 00:27:09
Module start 'content'
Timing: Jan 19 2025 00:27:09
Module end 'content'
Timing: Jan 19 2025 00:27:09
Script end

Main resources:

Total runtime0.6032 sec
Peak memory usage4,096.0000 KB
Database Queries52

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0071 588.0156152.6250
Module start 'layout' 0.00710.0038 740.640639.4453
Module start 'content' 0.01100.5904 780.0859464.5547
Module end 'content' 0.60140.0017 1,244.640612.1563
Script end 0.6032  1,256.7969 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00350.5858160.0002
Check MTime0.00140.2265160.0001
Mysql Total
Database connection0.00190.310010.0019
Mysqli_queries0.554791.9628520.0107
Looping result0.00050.0866500.0000
Template Total0.564793.620.2823
Template load0.00230.387120.0012
Template processing0.562393.220820.2812
Template load and register function0.00010.017410.0001
states
state_id_array0.00110.182910.0011
state_identifier_array0.00110.186820.0006
Override
Cache load0.00190.3163210.0001
Sytem overhead
Fetch class attribute can translate value0.00050.077110.0005
Fetch class attribute name0.00120.199230.0004
XML
Image XML parsing0.00050.087710.0005
class_abstraction
Instantiating content class attribute0.00000.001540.0000
General
dbfile0.00080.1312160.0000
String conversion0.00000.001840.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
2content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
2content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
5content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
3content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 14
 Number of unique templates used: 6

Time used to render debug report: 0.0001 secs