Old problem forgot answer

Author Message

Nathan Kelly

Sunday 02 July 2006 11:35:53 pm

Hi all,

long time no post but I'm back and expect to here more from me over the next few weeks...

I've been searching ez forums for the answer to a question I'm sure I asked in the past but I can't find it anywhere, I'm sure when I ask it it'll hit me like a wet brick in the face, but here goes.

How can I count the child nodes of a parent to find the last node?

Situation:

this is the same problem I have faced a number of times but I'm not sure how I overcame it in the past.

I have a (you guessed it) list... e.g.
<ul>
<li>item</li>
...
</ul>

I need to apply a class="last" attribute to the (you guessed it again... are you all psychic?) last list item. Can someone please refresh my memory how to do this?

I know how to fetch using the list_count etc. I'm just lost at the bit where I do the if else thing...

I knew you'd understand, what a great bunch of people you are ;)

Cheers!

Pardon me while I burst into flames...

Nathan Kelly

Sunday 02 July 2006 11:55:32 pm

I should just elaborate on that a little...

If I was to use a fetch like:

{def $count=fetch('content', 'list_count', hash('parent_node_id', $m_menu.node_id) )
		$sub_menu=fetch( 'content', 'list', hash( 'parent_node_id', $m_menu.node_id,
			'sort_by', array( array( 'priority' ) ),
			'class_filter_type', include,
			'class_filter_array', array('folder') ) ) }

Where $m_menu.node_id is the id of the parent which I have already fetched.

How can I take $count and find the last child?

I know there must be a simple way to do this but as I have been away from the ez template code for a few months I'm finding it quite hard to get back into the groove! Any ideas are most welcome.

Cheers!

Pardon me while I burst into flames...

Patrice DUCLAUD

Monday 03 July 2006 12:16:26 am

Hy,

You can perhaps try :

{fetch( 'content', 'list',
hash( 'parent_node_id', X,
'offset', 1,
'limit', 1 )}

Patrice

Nathan Kelly

Monday 03 July 2006 7:27:27 am

Hi Patrice,

Thanks for the input,I'm not sure that will find the last child node? But then again I'm a little rusty so I'll poke around the docs a bit to see if offsets and limits will help!

Cheers!

PS: All other input still welcome!

Pardon me while I burst into flames...

Nathan Kelly

Tuesday 04 July 2006 4:44:13 pm

Ok I've made some progress here but I'm one step off the solution and I can't figure it out.

For my navigation I'm running a request to find my top level nodes, and foreach of these top nodes I'm running a sub request for any children they may contain. Additionaly I'm counting the children with list_count.

My $count returns the number of children (at the moment this is 9).

I'm using:
{foreach $sub_menu as $index => $s_menu}

$index naturally returns 0 - 8.

My problem is that although 8 is the last child, the total number of children is 9 and I can't use offset on list_count, so I can't do something like {if $count|eq($index)}do something{/if}

Which is the sort of thing I need to be able to do, does anyone have any Ideas at all how I might do this? Is there any way to reduce the count by 1, this would work because the total count will always be returned 1 bigger than the index?

Any help really welcome.

Cheers!

Pardon me while I burst into flames...

Nathan Kelly

Tuesday 04 July 2006 5:47:48 pm

I got it, ignore my last post...

The answer for anyone interested:

				<ul id="main-menu">

{def $home=fetch('content', 'node', hash('node_id', $topnode_id) ) 
		$main_menu=fetch('content', 'list', hash('parent_node_id', 2,
			'sort_by', array( array( 'priority' ) ),
			'class_filter_type', include,
			'class_filter_array', array('folder') ) ) }

{def $m_count=$main_menu|count|dec()}

{foreach $main_menu as $m_index => $m_menu}

  					<li{if $m_index|eq($m_count)} id="last"{/if}><a{if eq($module_result.path[1].node_id,$m_menu.node_id)} class="current"{/if} href={$m_menu.url_alias|ezurl}><span><span>{$m_menu.name}</span></span></a>

{def $sub_menu=fetch( 'content', 'list', hash( 'parent_node_id', $m_menu.node_id,
			'sort_by', array( array( 'priority' ) ),
			'class_filter_type', include,
			'class_filter_array', array('folder') ) ) }

{def $s_count=$sub_menu|count|dec()}
			
{if $sub_menu|count|gt(0)}

						<ul class="sub-menu">

{foreach $sub_menu as $s_index => $s_menu}

							<li{if $s_index|eq($s_count)} class="last"{/if}><a href={$s_menu.url_alias|ezurl}>{$s_menu.name}</a></li>

{/foreach}

						</ul>
{/if}

					</li>
{/foreach}
{undef}

				</ul>

I was able to use the dec operator to deincrement the total count by one, now the count equals the last value of the indexed loop.

Hope this helps others.

Cheers!

Pardon me while I burst into flames...

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

Main resources:

Total runtime0.7435 sec
Peak memory usage4,096.0000 KB
Database Queries66

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0053 589.0313152.6250
Module start 'layout' 0.00530.0032 741.656339.4453
Module start 'content' 0.00850.7333 781.1016581.6328
Module end 'content' 0.74180.0016 1,362.734420.1641
Script end 0.7434  1,382.8984 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00320.4290160.0002
Check MTime0.00140.1849160.0001
Mysql Total
Database connection0.00100.134210.0010
Mysqli_queries0.674690.7338660.0102
Looping result0.00060.0796640.0000
Template Total0.706895.120.3534
Template load0.00210.275820.0010
Template processing0.704794.785220.3524
Template load and register function0.00010.012210.0001
states
state_id_array0.00100.132310.0010
state_identifier_array0.00070.097520.0004
Override
Cache load0.00180.2398620.0000
Sytem overhead
Fetch class attribute can translate value0.00050.063420.0002
Fetch class attribute name0.00120.159770.0002
XML
Image XML parsing0.00060.077620.0003
class_abstraction
Instantiating content class attribute0.00000.0031110.0000
General
dbfile0.00080.1119160.0001
String conversion0.00000.001140.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
5content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
6content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
11content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
3content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
2content/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: 29
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs