How smarty implements multi-level classification

Source: Internet
Author: User
This article mainly introduces how to implement multi-level classification by smarty. It involves loop reading techniques and is very useful. For more information, see

This article mainly introduces how to implement multi-level classification by smarty. It involves loop reading techniques and is very useful. For more information, see

This article describes 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. Single Loop is very simple, but 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.

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.