Home  >  Article  >  Backend Development  >  PHP method to get all subcategories under categories_PHP tutorial

PHP method to get all subcategories under categories_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:27:20761browse

php method to get all subclasses under the category

Get all subclass methods under the category:

static function getMenuTree($arrCat, $parent_id = 0, $level = 0,$all=True)

 {

static $arrTree; //Use static instead of global

 if(!$all) $arrTree ='';

 if( empty($arrCat)) return FALSE;

 $level++;

 if($level == 1) $arrTree[] = $parent_id;

foreach($arrCat as $key => $value)

 {

 if($value['parent_cid' ] == $parent_id)

 {

//$value[ 'level'] = $level;

$arrTree[] = $value['cid'];

unset($arrCat[$key]); //Unregister the current node data and reduce useless traversal

Self::getMenuTree($arrCat, $value[ 'cid'], $level);

 }

 }

return $arrTree;

 }

The prerequisite for using the above method is to write the classification into the cache file. The cache file writing method is as follows:

Public function actionIndex2()

 {

$filepath = Yii::getPathOfAlias('application').'/data/';

 $arr = array();

 $db = Yii::app()->db;

 $listinfo = $db->createCommand("select name,cid,parent_cid,root_cid from item_cat_info")->queryAll();

foreach($listinfo as $val)

 {

 $arr[$val['cid']] = array('cid'=>$val['cid'],'name'=>$val['name'],'parent_cid'=> ;$val['parent_cid'],'root_cid'=>$val['root_cid']);

 }

 $applist = "";

file_put_contents($filepath.'itemcat.php', $applist);

 }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/820413.htmlTechArticlephp Get all subclass methods under the category Get all subclass methods under the category: static function getMenuTree($arrCat , $parent_id = 0, $level = 0,$all=True) { static $arrTree;...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn