Preserve the order of nodes in fetch function

Author Message

Simone D'Amico

Friday 21 January 2011 9:04:22 am

Hi guys,

I've an array in ini file that contains the node ID of contents to display in one page. Something like this:

[PLPMenu]
MenuArray[]
MenuArray[]=121
MenuArray[]=60
MenuArray[]=61
MenuArray[]=62
MenuArray[]=63
MenuArray[]=111

To retrieve the content nodes, I've used the following code:

{def $menu = ezini('PLPMenu', 'MenuArray', 'plp.ini')}
{def $menu_items = fetch( 'content', 'node', hash( 'node_id', $menu ))}

It works fine but I need to preserve the order of nodes. The fetch function, instead, returns the nodes ordered by node ID. It returns the nodes in the order 60, 61, 62, 63, 111, 121 but I need the array order.

How can I preserve the right order of nodes?

Franck Magnan

Sunday 23 January 2011 7:36:48 am

Hello Simone,
it seems you made a mistakes in your template code:

  • The content/node fetch function retrieve a node and you use it to retrieve an array.
    In your code, $menu_item is not an array but a node.
  • The content/node fetch function needs a node_id in argument and you passed it $menu which is an array

If you want to get an array of nodes, you need to use such a code :

{def
     $menu = ezini('PLPMenu', 'MenuArray', 'plp.ini')
     $nodes = array()
     $current_node = false()
}
   {foreach $menu as $node_id}
     {set $current_node=fetch( 'content', 'node', hash( 'node_id', $node_id ))}
     {if is_object($current_node)}
       {set $nodes = $nodes|append($current_node)}
     {/if}
   {/foreach}
{undef $menu $nodes}

Then, in your config file, you can get an order if you use an index.
For example, you can modify your code to get:

[PLPMenu]
MenuArray[]
MenuArray[1]=121
MenuArray[2]=60
MenuArray[3]=61
MenuArray[4]=62
MenuArray[5]=63
MenuArray[6]=111

--
Developer at Open Wide

Simone D'Amico

Monday 24 January 2011 1:15:32 am

Hi Frank,

your post helped me a lot in finding the right solution!

Instead of calling content/fetch node function outside foreach loop, now i use the foreach with $menu values and, inside I use the fetch function with single node.

In this way I preserve the right order. :)

Thanks a lot! :)

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 18 2025 05:01:18
Script start
Timing: Jan 18 2025 05:01:18
Module start 'layout'
Timing: Jan 18 2025 05:01:18
Module start 'content'
Timing: Jan 18 2025 05:01:19
Module end 'content'
Timing: Jan 18 2025 05:01:19
Script end

Main resources:

Total runtime1.2154 sec
Peak memory usage4,096.0000 KB
Database Queries57

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0055 588.0469152.6406
Module start 'layout' 0.00550.0033 740.687539.4844
Module start 'content' 0.00881.2053 780.1719541.3516
Module end 'content' 1.21410.0013 1,321.523412.1250
Script end 1.2154  1,333.6484 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00300.2461160.0002
Check MTime0.00130.1030160.0001
Mysql Total
Database connection0.00090.071410.0009
Mysqli_queries1.163295.7029570.0204
Looping result0.00060.0455550.0000
Template Total1.174096.620.5870
Template load0.00190.156620.0010
Template processing1.172196.437020.5860
Template load and register function0.00020.016410.0002
states
state_id_array0.00130.104210.0013
state_identifier_array0.00170.141220.0009
Override
Cache load0.00160.1347290.0001
Sytem overhead
Fetch class attribute can translate value0.00040.036020.0002
Fetch class attribute name0.00150.124740.0004
XML
Image XML parsing0.00050.040520.0002
class_abstraction
Instantiating content class attribute0.00000.000740.0000
General
dbfile0.00140.1157150.0001
String conversion0.00000.000540.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
3content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
7content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
4content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
3content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1content/datatype/view/ezxmltags/li.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/li.tplEdit templateOverride template
1content/datatype/view/ezxmltags/ul.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/ul.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 22
 Number of unique templates used: 9

Time used to render debug report: 0.0001 secs