The data table structure of level Two classification is as follows:
The PHP code is as follows
Copy Code code as follows:
/**
@ article classification with two-level classification
@ param int $rootnum--Number of first class categories
@ param int $childnum--Number of categories at level two
@ return value Array
@ Date 2011.2.24
*/
function Temp_articletreecate ($rootnum, $childnum) {
if (!isnumber ($rootnum)) {
$rootnum = 10;
}
if (!isnumber ($childnum)) {
$childnum = 10;
}
$category = Array ();
$parent _sql = "Select Cateid,catename from". Table_prefix. " Articlecate WHERE parentid=0 and Depth=0 and flag=1 order by orders ASC ";
if (Intval ($rootnum) >0) {
$parent _sql.= "LIMIT $rootnum";
}
$parent _cate = $GLOBALS [' db ']->getall ($parent _sql);
foreach ($parent _cate as $parent _key => $parent _value) {
Subclass Array named Childcategory Custom name based on condition
$category [] = Array (' Cateid ' => $parent _value[' Cateid '), ' catename ' => $parent _value[' catename '], ' Childcategory ' =>array ());
Reading a child class
$child _sql = "Select Cateid,catename from". Table_prefix. " Articlecate WHERE parentid= ". $parent _value[' Cateid ']." and flag=1 ORDER by orders ASC ";
if (Intval ($childnum) >0) {
$child _sql.= "LIMIT $childnum";
}
$child _cate = $GLOBALS [' db ']->getall ($child _sql);
foreach ($child _cate as $child _key => $child _value) {
$category [Count ($category) -1][' childcategory '] = Array (' Cateid ' => $child _value[' Cateid '], ' catename ' =>$ child_value[' Catename ']);
}
}
return $category;
}
PHP page Call classification, such as index.php
Copy Code code as follows:
$goodscatetree = Array ();
$goodscatetree = Temp_goodstreecate (4,0); Call the classification function (including level two classification) 4--indicates that the first level category displays only 4, 0--indicates that the class two classification is limited in number
$tpl >assign ("Goodscatetree", $goodscatetree); Execute Smarty Engine
$tpl->display-> (INDEX.TPL); Output Smarty Stencil page
TPL template page Output category, such as Index.tpl page
Copy Code code as follows:
{section name=p loop= $goodscatetree}
First Class Category: {$goodscatetree [P].catename}
{section name=c loop= $goodscatetree [P].childcategory}
Class Two: {$goodscatetree [P].childcategory[c].catename}
{/section}
{/section}