PHP头条
热点:

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


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");
			}
		}
	}
模板代码:

	
		
{$sp.srvName}
{$pd.prod_cn}
|
当使用多重嵌套循环时,需要为每一个volist指定key值,通过
判断是否为数组中的最后一个元素。


www.phpzy.comtrue/phprm/43549.htmlTechArticleThinkPHP学习(四)volist标签高级应用之多重嵌套循环 Action代码: public function index(){$prod = I("get.prod_en");$id = I("get.id", 0, "int");if ($prod == ""){$serviceProduct = array();//多重循环遍历的数组 //数据...

相关文章

PHP之友评论

今天推荐