ThinkPHP学习(四)volist标签高级应用之多重嵌套循环 隔行变色

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

Action代码:

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

 <volist name="serviceProduct" id="sp" key="i">  <dl class="dlist odd">   <dt>{$sp.srvName}</dt>   <volist name="sp.product" id="pd" key="j">    <dd><a href="/index.php/question?prod_en={$pd.prod_en}">{$pd.prod_cn}</a></dd>    <if condition="$j lt count($sp['product'])">    <dd>|</dd>    </if>   </volist>   <if condition="count($sp['product']) EQ 0">    <dd> </dd>   </if>  </dl> </volist>
当使用多重嵌套循环时,需要为每一个volist指定key值,通过
<if condition="$j lt count($sp['product'])">
判断是否为数组中的最后一个元素。


Volist标签实现隔行变色

方法1:

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

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

方法2:
<volist name="newslist" id="vo" key="k">   <li <if condition="$k%2== '0'"> style="background-color:#000;"</if >><span>·<a href="{$vo.url}">{$vo.title}</a></span><span> {$vo.edittime|date="Y年m月d日",###}</span><span>{$vo.author}</span></li>   </volist>  
下面再列出一个 Volist 循环table里的tr、td的实例:

<tr bgcolor="#FBFCF1">   <volist name="siteurl" id="site" mod="4">  <eq name="mod" value="0"></tr><tr bgcolor="#FBFCF1"></eq>  <td width="25%"><a href="{$site.url}" target="_blank">{$site.name}</a></td>  </volist>  </tr>  




           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值