dounieqi6959 2013-06-21 15:12
浏览 59
已采纳

以下.tpl文件如何工作以显示表

Following is a code given in book

    {* products_list.tpl *}
    {load_presentation_object filename="products_list" assign="obj"}

      {if $obj->mProducts}
      <table class="product-list" border="0">
      <tbody>
      {section name=k loop=$obj->mProducts}
      {if $smarty.section.k.index % 2 == 0}
      <tr>
      {/if}
    <td valign="top">
    <h3 class="product-title">
    <a href="{$obj->mProducts[k].link_to_product}">
    {$obj->mProducts[k].name}
    </a>
    </h3>
    <p>
    {if $obj->mProducts[k].thumbnail neq ""}
    <a href="{$obj->mProducts[k].link_to_product}">
    <img src="{$obj->mProducts[k].thumbnail}"
    alt="{$obj->mProducts[k].name}" />
    </a>
    {/if}
    {$obj->mProducts[k].description}
    </p>

    <p class="section">
    Price:
    {if $obj->mProducts[k].discounted_price != 0}
    <span class="old-price">{$obj->mProducts[k].price}</span>
    <span class="price">{$obj->mProducts[k].discounted_price}</span>
    {else}
    <span class="price">{$obj->mProducts[k].price}</span>
    {/if}
    </p>
    </td>
    {if $smarty.section.k.index % 2 != 0 && !$smarty.section.k.first ||
    $smarty.section.k.last}
    </tr>
    {/if}
    {/section}
    </tbody>
    </table>
    {/if}

I am not clear with following line

  {if $smarty.section.k.index % 2 == 0}

why is the above statement written to see if index is an even number or odd number what effect does this have on table displayed?

  • 写回答

1条回答 默认 最新

  • duanhai7274 2013-06-21 15:19
    关注

    It's creating new table row every two array items.

     //open
     {if $smarty.section.k.index % 2 == 0}
       <tr>
     {/if}
    
     //close
     {if $smarty.section.k.index % 2 != 0 && !$smarty.section.k.first ||
         $smarty.section.k.last}
         </tr>
     {/if}
    

    Take a look at this example:

     echo "<table border='1'>
    ";
    
     for($i = 0; $i<=10; $i++) {
    
       if ($i % 2 == 0) {
           echo "<tr>
    ";
       }
    
       echo "<td> ". $i. "contents </td> 
    "; 
    
       if ($i % 2 != 0) {
           echo "</tr> 
    ";
       }
    
    }
    
     echo "<table />
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教