Using foreach
Smarty, and then use the Smarty Loop foreach to do a nested loop instance.
Two methods are very simple, the key is to construct a multidimensional array, and then loop the corresponding key name to reference ... is not very vague, see examples bar, bad analysis, examples intuitive good-looking, first look at the array, quoting the original article:
| code is as follows |
copy code |
$query =" Select Id,name,name_cn from Di_flag ORDER by id DESC "; $result = mysql_query ($query); while ($row = Mysql_fetch_array ($result)) { $query 2= ' select ID, name, Name_cn,flag from Di_sort WHERE Di_sort.flag = $row [id] ORDER BY id DESC "; $result 2=mysql_query ($query 2), while ($row 2 = mysql_fetch_array ($ RESULT2) { $post []=array (' Sid ' => $row 2[' id '], ' sortname ' => $row 2[' name '], ); } $row _array[] = Array (' CID ' => $row [' id '], ' cat_name ' => $row [' name '], ' topic ' => $post ); Unset ($post); } $smarty->assign ("forum", $row _array); unset ($row _array); |
Quite simply, the above is a two-dimensional array, and then look at how to nest loops, this time with foreach, look at the code:
| The code is as follows |
Copy Code |
{foreach from= $forum ietm=value} {$value. Cat_name} {foreach from= $value. Topic ietm=value2} |
using the section loop
One, PHP processing file output three-dimensional array code is as follows:
| The code is as follows |
Copy Code |
$brand _spaces = Array (); foreach ($category as $key => $value) { $condition = ' and B. ' RegionID ' = '. $regions->regionid. ' and M. ' Levelid ' = 1 and B. ' CategoryID ' = '. $value [' CategoryID ']. ' and B. ' ischecked ' = 1 '; $resourceid = $value [' CategoryID ']; $brand = $brandspaces->fetchbycondition (' 5 ', $condition, ' B. ' sort ' asc '); $brand _spaces[] = $brand; } Print_r ($brand _spaces); $smarty->assign (' brand_spaces ', $brand _spaces);
|
The array is reorganized by a Foreach loop query that conforms to the conditional result set.
Second, the processing file Print_r output three-dimensional array results are shown below:
| The code is as follows |
Copy Code |
Array ( [0] => Array ( [0] => Array ( [0] => 85 [ID] => 85 [=>] PHP Tutorial [CategoryName] => PHP Tutorial [=>]/u/342/logo/logo342.jpg [Logo] =>/u/342/logo/logo342.jpg [A] => PHP tutorial Example [title] => PHP Tutorial Example a ) [1] => Array ( [0] => 410 [ID] => 410 [=>] PHP Tutorial [CategoryName] => PHP Tutorial [=>]/u/398/logo/logo398.jpg [Logo] =>/u/398/logo/logo398.jpg [=>] MySQL database optimization [title] => MySQL Database optimization ) ) [1] => Array ( [0] => Array ( [0] => 9 [ID] => 9 [=>] SEO optimization [CategoryName] => seo optimization [=>]/u/228/logo/logo228.gif [Logo] =>/u/228/logo/logo228.gif [A] => seo/seo.html "target=" _blank "> Search engine Optimization techniques [title] => Search engine optimization skills ) [1] => Array ( [0] => 494 [ID] => 494 [=>] SEO optimization [CategoryName] => seo optimization [=>]/u/453/logo/logo453.gif [Logo] =>/u/453/logo/logo453.gif [A] => seo optimization of several major points of attention [title] => seo optimization of several major points of attention ) ) ) |
Third, in the template file through the Smarty section nested loop detailed analysis:
| The code is as follows |
Copy Code |
<!--{section Name=index loop= $brand _spaces}--> <div class= "Channel_i" > <a href= "#" title= "" class= "LEFTF" ><div><!--{$brand _spaces[index][0].categoryname}--></div ></a> <!--{section Name=customer loop= $brand _spaces[index]}--> <div class= "Brands_i" > <span class= "Pic_logo" ><a href= "#" title= "#" > "alt=" # "width=" 109 "height=" "/></a></span>" <p><a href= "#" ><!--{$brand _spaces[index][customer].title}--></a></p> </div> <!--{/section}--> </div> <!--{/section}--> |
1, $brand _spaces is the Assign name, $brand the length of the _spaces is 2, so the loop= $brand _spaces and loop=2 The final result is the same. loop is the variable name that determines the number of cycles.
2, <!--{$brand _spaces[index][0].categoryname}--> gets the outermost loop of the three-dimensional array and gets the value of [CategoryName]
3, $brand _spaces[index] decomposition open as: $brand _spaces[1] and $brand_spaces[2].
4, the following <!--{$brand _spaces[index][customer].logo}--> to get the value of [logo].
If the above look is not very obvious, in order to facilitate understanding, you can also set a variable: $brand = $brand _spaces[index];
That is, the section internal loop is:<!--{section Name=customer loop= $brand}-->
That is, to get [logo] value of <!--{$brand [Customer].logo}--> this is the same as the regular section of the cycle, it is very good to understand.