ThinkPHP learning (4) Multiple nested loops for advanced applications of volist tags
Release: 2016-07-25 09:12:27
Original
1101 people have browsed it
Action code:
- public function index(){
- $prod = I("get.prod_en");
- $id = I("get.id", 0, "int");
- if ($prod == ""){
- $serviceProduct = array();//Array traversed by multiple loops
Copy code
- //The data is saved in two tables, here $serviceProduct is initialized through the loop Array
- $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 code
- //If you want to use it in the template To output variables, the variables must be passed to the template in the controller. The system provides the assign method to assign values to template variables. Regardless of the variable type, assign assignment is used 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");
- }
- }
- }
-
Copy code Template code:
-
-
- {$sp.srvName}
-
-
- {$pd.prod_cn}
-
-
- |
-
-
-
-
-
-
-
- Specify the key value and use
Copy the code to determine whether it is the last element in the array. -
|
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31