Smarty method of realizing multilevel classification _php skills

Source: Internet
Author: User

This paper illustrates the method of Smarty to realize multilevel classification. Share to everyone for your reference. The specific analysis is as follows:

Here a simple introduction to the use of PHP smarty multilevel classification Read and loop method, single cycle is very simple, but the multi-level will be more complex, let's look at the following, interested friends can look at:

Copy Code code 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 '] is the category id,parent_id is the sub-category owning ID
$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:
Copy Code code as follows:
<ul>
{foreach Name=cat from= $class item=cat}
<li><strong>{$cat. name}</strong>[{$cat .id}]</li>
<ul style= "padding-left:20px;" >
{foreach Name=subcat from= $subclass item=subcat}
{if $subcat. parent_id== $cat. ID}
<li>{$subcat. name}[{$subcat .parent_id}]</li>
{/if}
{/foreach}
</ul>
{/foreach}
</ul>

The results of the operation are shown in the following illustration:

I hope this article will help you with your PHP program design.

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.