zoukankan      html  css  js  c++  java
  • ThinkPHP学习 volist标签高级应用之多重嵌套循环、隔行变色(转)

    Action代码:

    [php]
     
    1.   public function index(){  
    2. $prod = I("get.prod_en");  
    3. $id = I("get.id", 0, "int");  
    4. if ($prod == ""){  
    5.     $serviceProduct = array();//多重循环遍历的数组  
    [php] 
     
    1. //数据保存在两张表中,这里通过循环初始化$serviceProduct数组  
    2.             $service = M("product_class")->order("oid ASC")->select();  
    3.             for ($i = 0; $i < count($service); $i++)  
    4.             {  
    5.                 array_push($serviceProduct, array("srvName"=>$service[$i]["pc_cn"], "product"=>M("product")->where("prod_class_id=".$service[$i]["pcid"])->order("oid ASC")->select()));  
    6.             }  
    [php] 
     
    1. //如果要在模板中输出变量,必须在在控制器中把变量传递给模板,系统提供了assign方法对模板变量赋  
    2. 值,无论何种变量类型都统一使用assign赋值。  
    3.             $this->assign("serviceProduct", $serviceProduct);  
    4.             $this->display();  
    5.         }else{  
    6.             if ($id > 0){  
    7.                 $this->display("detail");  
    8.             }else{  
    9.                 $this->assign('prod_en', $prod);  
    10.                 $clsList = M("question_class")->order("oid ASC")->select();  
    11.                 $this->assign('clsList', $clsList);  
    12.                   
    13.                 $qusList = M("question")->order("oid ASC")->select();  
    14.                 $this->assign('qusList', $qusList);  
    15.                 $this->display("list");  
    16.             }  
    17.         }  
    18.     }  

    模板代码:

    [html] 
     
    1. <volist name="serviceProduct" id="sp" key="i">  
    2.     <dl class="dlist odd">  
    3.         <dt>{$sp.srvName}</dt>  
    4.         <volist name="sp.product" id="pd" key="j">  
    5.             <dd><href="/index.php/question?prod_en={$pd.prod_en}">{$pd.prod_cn}</a></dd>  
    6.             <if condition="$j lt count($sp['product'])">  
    7.             <dd>|</dd>  
    8.             </if>  
    9.         </volist>  
    10.         <if condition="count($sp['product']) EQ 0">  
    11.             <dd</dd>  
    12.         </if>  
    13.     </dl>  
    14. </volist>  

    当使用多重嵌套循环时,需要为每一个volist指定key值,通过

    [html] 
     
    1. <if condition="$j lt count($sp['product'])">  

    判断是否为数组中的最后一个元素。

    Volist标签实现隔行变色

    方法1:

    [html] 
     
    1. <volist name="newslist" id="vo" mod="2">    
    2.  <li <eq name="mod" value="0"style="background-color:#000;"</eq>><span<href="{$vo.url}">{$vo.title}</a></span><span>{$vo.edittime|date="Y年m月d日",###}</span><span>{$vo.author}</span></li>    
    3. </volist>  

    volist 中的 mod 参数相当于指定一个频率,系统会将当前的实际记录对 mod 参数值求余(PHP中的%运算符)运算。而配合判断标签(如eq标签),就可以按照频率控制输出的数据或数据显示的格式。

    方法2:

    [html] 
     
    1. <volist name="newslist" id="vo" key="k">    
    2.  <li <if condition="$k%2== '0'"style="background-color:#000;"</if >><span<href="{$vo.url}">{$vo.title}</a></span><span> {$vo.edittime|date="Y年m月d日",###}</span><span>{$vo.author}</span></li>    
    3.  </volist>    

    下面再列出一个 Volist 循环table里的tr、td的实例:

    [html] 
     
      1. <tr bgcolor="#FBFCF1">    
      2.  <volist name="siteurl" id="site" mod="4">    
      3. <eq name="mod" value="0"></tr><tr bgcolor="#FBFCF1"></eq>    
      4. <td width="25%"><href="{$site.url}" target="_blank">{$site.name}</a></td>    
      5. </volist>    
      6. </tr>    
  • 相关阅读:
    解决bash: less: command not found
    IDEA-相关插件使用
    如何理解多租户架构?
    mybatis自动生成model、dao及对应的mapper.xml文件
    IDEA设置提示生成序列化ID
    [DUBBO] qos-server can not bind localhost:22222错误解决
    @NotNull,@NotEmpty,@NotBlank区别
    (三)IDEA使用,功能面板
    PHP实现自己活了多少岁
    使用PHP函数输出前一天的时间和后一天的时间
  • 原文地址:https://www.cnblogs.com/snowhite/p/4913924.html
Copyright © 2011-2022 走看看