Home  >  Article  >  Backend Development  >  How smarty implements i

How smarty implements i

WBOY
WBOYOriginal
2016-06-27 13:18:091357browse

How to implement i in html page with smarty
How to translate the following C# code into smarty code?

<%                   int i_count = 0;                    foreach (GridViewRow row in this.GridView1.Rows)                   {                          if (i_count%4 == 0)                          {%>                                 <div>.1..<div><%                          }                          else                           {%>                                 <div>.2..<div><%                            }                           i_count ++ ;                    }%>



This is what I wrote, but ERROR
                   {assign var="i_count"}                   {foreach from=$row item=item}                                             {if $i_count mod 4}                                                           <div>.2..<div>                                                  {else}                                                            <div>.1..<div>                           {/if}                           {$i_count + 1 }                    {/foreach}


How do I write it? Did I write i wrong?


Reply to discussion (solution)

Why do you write it like this? In smarty foreach, you can get the current key {foreach from=$var key=k item=v}
This k is the one. Haha

{assign var="i_count" value=0}                   {foreach from=$row item=item}                                              {if $i_count mod 4}                                                            <div>.2..<div>                                                   {else}                                                             <div>.1..<div>                           {/if} {assign var="i_count" value=$i_count+1}                    {/foreach}

I haven’t tested it. If it doesn’t work, just use $i_count 1 Use `` to cause




Plain Text code?1234567891011121314{assign var="i_count" value=0}                                                                                                           i_count mod 4}…

It seems to be caused by '', but why did my code execute the first div in the first div, and the contents of the second div in the second DIV later?

                    {if $i_count mod 4 == 0}                  		<div>.1..<div>                   {else}                   		<div>.2..<div>                   {/if} 

{foreach from=$row item=item key=i_count}                                            {if $i_count mod 4}                                                             <div>.2..<div>                                                    {else}                                                              <div>.1..<div>                           {/if}

{foreach from=$row item=item key=i_count}{if ($i_count mod 4)==0}<div>.2..<div>{else}<div>.1..<div>{/if}

This sentence can be written like this.

{if ($i_count mod 4) eq 0}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn