ThinkPHP learning (4) volist tag multiple nested loop Action code for advanced applications: publicfunctionindex () {$ prodI (& quot; get. prod_en & quot;); $ idI (& quot; get. id & quot;, 0, & quot; int & quot;); if ($ prod & quot; & ThinkPHP learning (4) multiple nested loops of advanced volist labels
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
// Data is saved in two tables. here, $ serviceProduct array $ service = M ("product_class")-> order ("oid ASC")-> select () is initialized cyclically (); 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 the template, you must pass the variables to the template in the controller. The system provides the assign method to assign values to the template variables. all variable types use The assign method to assign values. $ 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 ('cllist', $ clsList); $ qusList = M ("question ") -> order ("oid ASC")-> select (); $ this-> assign ('quslist', $ qusList ); $ this-> display ("list ");}}}
Template code:
-
{$sp.srvName}
-
{$pd.prod_cn}
-
|
-
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.