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 { 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); } } }
复制代码
模板代码:
{$sp.srvname} {$pd.prod_cn} |
复制代码
当使用多重嵌套循环时,需要为每一个volist指定key值,通过
复制代码
判断是否为数组中的最后一个元素。