This article mainly introduces how smarty implements multi-level classification. it involves loop reading techniques and is very useful. if you need it, you can refer to the example below to illustrate how smarty implements multi-level classification. Share it with you for your reference. The specific analysis is as follows:
Here is a brief introduction to the use of php smarty multi-level classification reading and loop methods. a single loop is simple, but the multi-level reading is complicated. let's take a look at it. interested friends can look at it:
The code is as follows:
<? Php
$ Result = mysql_query ("SELECT id, description, parent_id, name, sort_order FROM article_categoey WHERE parent_id = 0 order by id ");
While ($ row = mysql_fetch_array ($ result )){
$ Class [] = $ row;
// $ Row ['id'] indicates the Category id and parent_id indicates the id of the subcategory.
$ Result_sub = mysql_query ("SELECT id, description, parent_id, name, sort_order FROM article_categoey WHERE parent_id = '". $ row ['id']. "'Order by id ");
While ($ row_sub = mysql_fetch_array ($ result_sub )){
$ Subclass [] = $ row_sub;
}
$ Smarty-> assign ("subclass", $ subclass );
}
$ Smarty-> assign ("class", $ class );
$ Smarty-> display('articles.html ');
?>
The HTML code is as follows:
The code is as follows:
{Foreach name = cat from = $ class item = cat}
- {$ Cat. name}[{$ Cat. id}]
{Foreach name = subcat from = $ subclass item = subcat}
{If $ subcat. parent_id = $ cat. id}
- {$ Subcat. name} [{$ subcat. parent_id}]
{/If}
{/Foreach}
{/Foreach}
Shows the running result:
I hope this article will help you with PHP programming.