Home > Backend Development > PHP Tutorial > ThinkPHP Learning (4) Multiple Nested Loops for Advanced Application of Volist Tag_PHP Tutorial

ThinkPHP Learning (4) Multiple Nested Loops for Advanced Application of Volist Tag_PHP Tutorial

WBOY
Release: 2016-07-13 10:30:09
Original
846 people have browsed it

Action code:

    public function index(){
		$prod = I("get.prod_en");
		$id = I("get.id", 0, "int");
		if ($prod == ""){
			$serviceProduct = array();//多重循环遍历的数组
Copy after login
//数据保存在两张表中,这里通过循环初始化$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()));
			}
Copy after login
//如果要在模板中输出变量,必须在在控制器中把变量传递给模板,系统提供了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");
			}
		}
	}
Copy after login
Template code:

	<volist name="serviceProduct" id="sp" key="i">
		<dl class="dlist odd">
			<dt>{$sp.srvName}</dt>
			<volist name="sp.product" id="pd" key="j">
				<dd>{$pd.prod_cn}</dd>
				<if condition="$j lt count($sp['product'])">
				<dd>|</dd>
				</if>
			</volist>
			<if condition="count($sp['product']) EQ 0">
				<dd> </dd>
			</if>
		</dl>
	</volist>
Copy after login
When using multiple nested loops, You need to specify a key value for each volist, and use
<if condition="$j lt count($sp['product'])">
Copy after login
to determine whether it is the last element in the array.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/767112.htmlTechArticleAction code: public function index(){$prod = I("get.prod_en");$id = I("get.id", 0, "int");if ($prod == ""){$serviceProduct = array();//Array traversed by multiple loops//The data is saved in...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template