ThinkPHP learning (4) multiple nested loops of advanced volist labels

Source: Internet
Author: User
ThinkPHP learning (4) multiple nested loops of advanced volist labels

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 for multi-loop traversal

  1. // The data is saved in two tables. here, the $ serviceProduct array is initialized cyclically.
  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. }

  1. // If you want to output variables in the template, you must pass the variables to the template in the controller. The system provides the assign method to assign the Template variables
  2. Value, regardless of the variable type, all use assign value assignment.
  3. $ This-> assign ("serviceProduct", $ serviceProduct );
  4. $ This-> display ();
  5. } Else {
  6. If ($ id> 0 ){
  7. $ This-> display ("detail ");
  8. } Else {
  9. $ This-> assign ('prod _ EN', $ prod );
  10. $ ClsList = M ("question_class")-> order ("oid ASC")-> select ();
  11. $ This-> assign ('cllist', $ clsList );
  12. $ QusList = M ("question")-> order ("oid ASC")-> select ();
  13. $ This-> assign ('quslist', $ qusList );
  14. $ This-> display ("list ");
  15. }
  16. }
  17. }

Template code:

  1. {$ Sp. srvName}
  2. {$ Pd. prod_cn}
  3. |

When multiple nested loops are used, you must specify the key value for each volist.

Determines whether it is the last element in the array.


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.