Forums / Setup & design / adding up the value of one specific attribute of all the children in one node

adding up the value of one specific attribute of all the children in one node

Author Message

Nicolas Heiringhoff

Monday 26 April 2004 4:10:21 am

We have a folder (Node ID 47) with the children A, B, C, D, E,...

A (Node ID 118) - B (Node ID 119) - C (Node ID 120) - D (Node ID 121) - E (Node ID 122)
all these nodes are objects of a class called 'frabo' with the attribute "umsatz" and attribute id 200

Our goal is to get the sum of all attributes with the attribute id 200.

With this code we can show all the integer-values of the atribute "umsatz" of all children in node 47

{let nodes=fetch( 'content', 'tree', hash( 'parent_node_id', 47 ) ) }

{section loop=$nodes}
<br />
{$:item.data_map.umsatz.data_int}
{/section}

{/let}

with the following code we can add two or more values together:

{let ergebnis=hash('wert 1', 6, 'wert 2', 4)}
{$ergebnis|sum}
{/let}

now we want to add all these "umsatz"-values together. we have tried for three days, but we can not get theses two codes working together. has anybody a solution for the problem?

thanx from germany

http://www.heiringhoff.de

Balazs Halasy

Monday 26 April 2004 4:36:49 am

Look at the code that is presented here:

http://ez.no/ez_publish/documentation/building_an_ez_publish_site/the_guestbook/creating_the_template

Feel free to copy & use it. If so: change the line that says

{set counter=$:counter|inc}

to something like this:

{set counter=$:counter|sum($:item.object.data_map.umsatz)}

Balazs

Nicolas Heiringhoff

Monday 26 April 2004 6:05:34 am

we have modified the code the way you suggested:


{* Grab all the guestbook entries. *}
{let name=test counter=0 children=fetch( content,
                                         list,
                                         hash( parent_node_id,
                                               $node.node_id,
                                               sort_by,
                                               $node.sort_array
                                             )
                                        )
                                       
                                        
}
 Value of counter at this point: {$:counter}<br />
 
<table>

{* Loop through all the entries. *}
{section loop=$:children}
<br />
    {* Increment the counter by one. *}
    
{set counter=$:counter|sum($:item.object.data_map.umsatz)}   


<tr><td>

        Umsatz value: <i>{$:item.object.data_map.umsatz.content|wash}</i>
    </td></tr>

{* End of loop. *}
{/section}

    <tr><td><hr /></td></tr>
</table>

<div class="center">
    total Umsatz: {$:counter}
</div>

{* Release the counter and the children variable. *}
{/let}

It returns the values of the 4 objects of the class "frabo" in node 47:
Umsatz value: 1

Umsatz value: 12500

Umsatz value: 20000

Umsatz value: 500000

but wenn the code should return the sum of the "total umsatz" , it returns:

total Umsatz: 24

now that is rather strange, because all we don?t know where this value '24' comes from.
the total umsatz should add up to 532 501.

http://www.heiringhoff.de

Nicolas Heiringhoff

Wednesday 28 April 2004 6:54:33 am

After two days we finally got it:

{let counter=0}
	
			{let nodes=fetch( 'content', 'tree', hash( 'parent_node_id', 47 ) ) }
        	{section loop=$nodes}
        
			{set counter=$:counter|sum($:item.data_map.umsatz.data_int,$counter)}		
			

			{/let}
	
			{/section}
			
<!-- hier wird das Ergebnis ausgegeben-->

			{$counter}
			
<!-- hier wird das Ergebnis ausgegeben-->
{/let}

http://www.heiringhoff.de