简体   繁体   中英

PHP : How to use smarty section for the following output?

I am using the even and odd logic for that but not getting the output as i need

for example i have array of

$data = array(1000,1001,1002, 1003,1004,1005);
$smarty->assign('data',$data);

{section name=i loop=$data}

{section}

so the output i need is :

<div>
    <dl>1000</dl>
    <dl>1001</dl>
</div>

<div>
    <dl>1002</dl>
    <dl>1003</dl>
</div>

<div>
    <dl>1004</dl>
    <dl>1005</dl>
</div>

According to offical document http://www.smarty.net/docsv2/en/language.function.section.tpl

Try following codes:

<div>
{section name=i loop=$data}

       <dl>{$data[i]}</dl>

   {if $smarty.section.data.index > 0 && $smarty.section.data.index % 2 == 0 && $smarty.section.data.index < $smarty.section.customer.total -1}
      </div><div>
   {/if}

{/section}
</div>

You should do it this way:

{section name=i loop=$data}
    {if $smarty.section.i.index %2 == 0}
        <div>
    {/if}
       <dl>{$data[i]}</dl>
    {if $smarty.section.i.index %2 == 1 || $smarty.section.i.last}
        </div>
    {/if}
{/section}

<div> should be rather created in section because when there weren't be any items you probably wouldn't like to create empty <div> . Last condition $smarty.section.i.last is added in case you have for example 5 elements and in that case you of course make sure your div is closed.

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