Home  >  Article  >  Backend Development  >  ThinkPHP learning (4) Multiple nested loops for advanced applications of volist tags

ThinkPHP learning (4) Multiple nested loops for advanced applications of volist tags

WBOY
WBOYOriginal
2016-07-25 09:12:271057browse

Action code:

  1. public function index(){
  2. $prod = I("get.prod_en");
  3. $id = I("get.id", 0, "int");
  4. if ($prod == ""){
  5. $serviceProduct = array();//Array traversed by multiple loops
Copy code
  1. //The data is saved in two tables, here $serviceProduct is initialized through the loop Array
  2. $service = M("product_class")->order("oid ASC")->select();
  3. for ($i = 0; $i < count($service); $i++)
  4. {
  5. array_push($serviceProduct, array("srvName"=>$service[$i]["pc_cn"], "product"=>M("product")->where("prod_class_id=".$ service[$i]["pcid"])->order("oid ASC")->select()));
  6. }
Copy code
  1. //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.
  2. $this->assign("serviceProduct", $serviceProduct);
  3. $this->display();
  4. }else{
  5. if ($id > 0){
  6. $this->display("detail ");
  7. }else{
  8. $this->assign('prod_en', $prod);
  9. $clsList = M("question_class")->order("oid ASC")->select();
  10. $this->assign('clsList', $clsList);
  11. $qusList = M("question")->order("oid ASC")->select();
  12. $this-> assign('qusList', $qusList);
  13. $this->display("list");
  14. }
  15. }
  16. }
Copy codeTemplate code:

  1. {$sp.srvName}
  2. {$pd.prod_cn}
  3. |
  4. Specify the key value and use
Copy the code
    to determine whether it is the last element in the array.



Statement:
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