Forums / Developer / Old problem forgot answer

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...