Working with data in a Loop

Author Message

Matt Dorn

Monday 27 October 2003 1:40:08 am

Maybe somebody can help me.

As a Web developer on other platforms, I've been in many situations where it made sense to sort data on the basis of some category field, then display the data broken down by that category.

For example, let's say I have a 5-record result set that looks like this:

Company: Productos AC
Country: Spain

Company: ABC, Inc.
Country: U.S.

Company: XYZ Gmbh.
Country: Germany

Company: Acme Enterprises
Country: U.S.

Company: Gonzales, SA
Country: Spain

Now, if I return this data sorted by country, in a language like PHP I could create a loop, and store the country name in a buffer variable before moving to the next iteration, then I'd compare the buffer country value to the current country value, and if it's different, create a delimiter (---------) in the display. So the output would look like this:

XYZ Gmbh. - Germany
------------
Gonzales SA - Spain
Productos AC - Spain
------------
ABC Inc. - U.S.
Acme Enteprises - U.S.

In EZPublish, while I can sort the data appropriately, it seems I don't have enough control within the looping structure to accomplish this.

I have a loop that looks like this:


    {section name=Child loop=fetch(content, list, hash( 
        parent_node_id, $node.node_id,
        limit, $page_limit,
        offset, $view_parameters.offset,
        sort_by,array(array('attribute',true(),215)),
        class_filter_type, include, 
        class_filter_array, array( 18 ) ) )}
        
        $Child:item.data_map.country.data_text}
        
    {/section}

Is there anything I can do here to accomplish my goal?

Vivienne van Velzen

Monday 27 October 2003 3:46:38 am

Hi Matt,

Couldn't you do something like this:

{let name=myroot
     mytext=""
}
{section loop=fetch...}
    {section show=$myroot:mytext|ne($:item.attribute)}
        ----------<br>
    {/section}
    {$:item.data_map.country.data_text}<br>
    {set mytext=$:item.attribute}
{/section}
{/let}

Where $mytext is the buffer variable you mentioned. Note that I moved the namespace (the section no longer has a name, the surrounding let has), so the 'set' function works.
Hope this helps,

Vivienne

Matt Dorn

Monday 27 October 2003 9:38:05 am

Vivienne, you are a master! I guess my main problem was not knowing how to manipulate the namespace properly (I assumed you could nest them, and still be able to set the variables in the parent namespace).

Anyway, this works perfectly. Thanks so much for your help!

Matt

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