简体   繁体   中英

How to increment variable using foreach in smarty?

This is my assigned variable

 {assign var="count" value=0}

I put the variable in foreach statement

{foreach from=$name item=names}
   {assign var="count" value=$count+1}
{/foreach}

Want to display variable in div named "friendsCount"

<div class="friendsCount">
   Total friends:{$count}
</div>

But when the program error message is displaying " Trying to get property of non-object". Please help me to find a solution. Am using codeigniter

You don't need "" when you declare variable in smarty. You can simply declare like this.

{assign var=count value=0}

{foreach from=$name item=names}
    {assign var=count value=$count+1}
{/foreach}

You can count total items of array with {$friends|count}

Documentation: http://smarty.incutio.com/?page=SmartyFrequentlyAskedQuestions#template-1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM