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: Feb 03 2025 03:52:23
Script start
Timing: Feb 03 2025 03:52:23
Module start 'layout'
Timing: Feb 03 2025 03:52:23
Module start 'content'
Timing: Feb 03 2025 03:52:23
Module end 'content'
Timing: Feb 03 2025 03:52:23
Script end

Main resources:

Total runtime0.0159 sec
Peak memory usage4,096.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0054 591.6016152.6328
Module start 'layout' 0.00540.0033 744.2344216.1406
Module start 'content' 0.00870.0052 960.3750204.7266
Module end 'content' 0.01390.0019 1,165.101634.2891
Script end 0.0159  1,199.3906 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.003119.3853140.0002
Check MTime0.00159.6312140.0001
Mysql Total
Database connection0.00084.787810.0008
Mysqli_queries0.001710.930130.0006
Looping result0.00000.081010.0000
Template Total0.00169.910.0016
Template load0.00095.504810.0009
Template processing0.00074.340810.0007
Override
Cache load0.00063.976410.0006
General
dbfile0.00031.780480.0000
String conversion0.00000.045040.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs