Thinkphp Learning (iv) multiple nested loops for advanced application of Volist label

Source: Internet
Author: User
Tags array count key variable

Action Code:

    The Public Function index () {
		$prod = I ("get.prod_en");
		$id = I ("Get.id", 0, "int");
		if ($prod = = "") {
			$serviceProduct = array ();//Multiple Loops traversal array
The data is stored in two tables, where the $serviceproduct array is initialized by looping
			$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 ());
			}
If you want to output variables in a template, you must pass the variable to the template in the controller, and the system provides a assign method to assign values to the template variable
, regardless of the type of variable that uses the Assign assignment uniformly.
			$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");}}
	
Template code:
	
 
  
		
  
   
  
{$sp. Srvname}
{$pd. PROD_CN}
When using multiple nested loops, you need to specify a key value for each volist, by
 
  
Determines whether the last element in the array is the same.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.