Home  >  Article  >  Backend Development  >  How to get all subcategories under a category in php_php cache file writing

How to get all subcategories under a category in php_php cache file writing

WBOY
WBOYOriginal
2016-07-25 08:52:16735browse
  1. static function getMenuTree($arrCat, $parent_id = 0, $level = 0,$all=True)
  2. {
  3. static $arrTree; //Use static instead of global
  4. if(!$ all) $arrTree = '';
  5. if(empty($arrCat)) return FALSE;
  6. $level++;
  7. if($level == 1) $arrTree[] = $parent_id;
  8. foreach($arrCat as $key = > $value)
  9. {
  10. if ($value['parent_cid' ] == $parent_id)
  11. {
  12. //$value[ 'level'] = $level;
  13. $arrTree[] = $value['cid' ];
  14. unset($arrCat[$key]); //Unregister the current node data and reduce useless traversal
  15. self::getMenuTree($arrCat, $value[ 'cid'], $level);
  16. } // www.plcxue.com
  17. }
  18. return $arrTree;
  19. }
Copy code

Using the above method, you need to write the classification into the php cache file.

2. Cache file writing method:

  1. public function actionIndex2()
  2. {
  3. $filepath = Yii::getPathOfAlias('application'). '/data/';
  4. $arr = array();
  5. $db = Yii::app()->db;
  6. $listinfo = $db->createCommand("select name,cid,parent_cid,root_cid from item_cat_info")->queryAll();
  7. foreach($listinfo as $val)
  8. {
  9. $arr[$val['cid']] = array('cid'=>$val['cid'], 'name'=>$val['name'],'parent_cid'=>$val['parent_cid'],'root_cid'=>$val['root_cid']);
  10. }
  11. $applist = " ";
  12. file_put_contents($filepath.'itemcat.php', $applist);
  13. }
Copy code


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