Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

count number of SUB array... ?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Mon Jan 04, 2016 5:42 am    Post subject: count number of SUB array... ? Reply with quote

Hello.

I do know how to use the count feature

if $myarray|@count >0 etc...

I am wondering if its possible to do a 'count' of a sub array ?

$myarray.name1
$myarray.name2.population
$myarray.name3.population
$myarray.name4

Now myarray.name2 & name3 both have a 'population' sub-array within them.

I am already doing a 'foreach' loop to display the main content below... But I am wanting to adda 'header' lime in the table, & need a yes/no answer as to whether 'population' exists in the main table below.


I COULD do another 'foreach' loop to do the 'header' but i'll rather not, as it wastes processing time (will it?)

PS - the 'name' field is not standardised - Ive only used it in the above example. I do not know the name of "name" in advance, but i do know there can be a "population" field / array (did that make sense ?)
Back to top
View user's profile Send private message
elpmis
Smarty Elite


Joined: 07 Jun 2007
Posts: 321

PostPosted: Mon Jan 04, 2016 11:34 am    Post subject: Reply with quote

Not sure if I know what you want but have you tried something like

Code:
{if $myarray.name2.population|@count >0}


By the way - you should find another method to hide output ... "@" to suppress errors is a real performance eater.
Back to top
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Tue Jan 05, 2016 6:36 am    Post subject: Reply with quote

[quote="elpmis"]

[code]{if $myarray.name2.population|@count >0}[/code]
[/quote]

Thats ok, if i know "name2" in advance - but I do not.

It could be 20-30 different unknown names..

{if $myarray.name2.population|@count >0}

{if $myarray.biggles.population|@count >0}

{if $myarray.turnips.population|@count >0}

{if $myarray.carrots.population|@count >0}

{if $myarray.mrbig.population|@count >0}

{if $myarray.places.population|@count >0}


I'm guessing theres not a simple 1-liner or so. ? I have it going, with a foreach loop to do this... - But appears inefficient... (but going)


[quote="elpmis"]
By the way - you should find another method to hide output ... "@" to suppress errors is a real performance eater.
[/quote]

What do you mean ? (looking as to where I have used @).
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Jan 05, 2016 10:56 pm    Post subject: Reply with quote

sarahfoxnz wrote:
{if $myarray.name2.population|@count >0}

{if $myarray.biggles.population|@count >0}

{if $myarray.turnips.population|@count >0}

{if $myarray.carrots.population|@count >0}

{if $myarray.mrbig.population|@count >0}

{if $myarray.places.population|@count >0}

What do you mean ? (looking as to where I have used @).

You really don't see?
Back to top
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Wed Jan 06, 2016 5:34 am    Post subject: Reply with quote

Original :-

Quote:

hide output ... "@" to suppress errors is a real performance eater


I'm not trying to hide output... or suppress errors... - that's why i'm confused..

I have found this and a dozen other websites to say to use @count as a way to count the number of entries in an array.

http://stackoverflow.com/questions/9134835/get-total-number-of-items-in-array

Quote:
{$array|@count}


if that is not the correct way, please advise the correct way (without doing a foreach loop & manually counting)..
Back to top
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Wed Jan 06, 2016 5:56 am    Post subject: Reply with quote

$array.2ndfield.3rdfield

Ive moved on with this. I'm guessing there's no 1-liner command to find if '3rdfield' exists (count >0) without knowing the name of 2ndfield in advance.

We need to do a loop over the whole array & work our way down to the 3rd array.

I've moved on, doing a loop.

but I am interested in the correct way to 'count'..
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Jan 06, 2016 6:55 am    Post subject: Reply with quote

Why do you use "dozens of other websites" and ignore the only aurthoritative source of information about Smarty?…
Back to top
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Wed Jan 06, 2016 10:22 am    Post subject: Reply with quote

AnrDaemon wrote:
Why do you use "dozens of other websites" and ignore the only aurthoritative source of information about Smarty?…


http://www.smarty.net/docs/en/language.modifiers.tpl

Quote:

{* php's count *}
{$myArray|@count}


The above quote is from the smarty.net website. i use Google because its faster to search..

If your authorised website is wrong - Please advise.


EDIT - I see below that from version 3 of Smarty, we no longer need @

im on 2.6.26

(I'll try to remember - if i upgrade)
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Jan 07, 2016 4:32 am    Post subject: Reply with quote

Instead of blindly copying the code, you should understand its meaning and write your own code based on provided examples.
Glad you've found the note by yourself though. Please keep it up, you may yet make it past copypaste into a real programming.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP