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

eZ debug

Timing: Jan 18 2025 15:13:42
Script start
Timing: Jan 18 2025 15:13:42
Module start 'content'
Timing: Jan 18 2025 15:13:42
Module end 'content'
Timing: Jan 18 2025 15:13:42
Script end

Main resources:

Total runtime0.1498 sec
Peak memory usage2,048.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0048 587.7109180.8359
Module start 'content' 0.00480.0065 768.5469101.9922
Module end 'content' 0.01130.1384 870.5391531.2734
Script end 0.1497  1,401.8125 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00332.1875200.0002
Check MTime0.00140.9052200.0001
Mysql Total
Database connection0.00070.454610.0007
Mysqli_queries0.109172.86061410.0008
Looping result0.00130.89901390.0000
Template Total0.138192.210.1381
Template load0.00080.512110.0008
Template processing0.137391.657610.1373
Override
Cache load0.00050.349210.0005
Sytem overhead
Fetch class attribute can translate value0.00080.550110.0008
XML
Image XML parsing0.00050.328410.0005
General
dbfile0.00382.5695200.0002
String conversion0.00000.002530.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/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