Heim  >  Artikel  >  Backend-Entwicklung  >  php怎么获取分类下所有子类_php缓存文件写入

php怎么获取分类下所有子类_php缓存文件写入

WBOY
WBOYOriginal
2016-07-25 08:52:16735Durchsuche
  1. static function getMenuTree($arrCat, $parent_id = 0, $level = 0,$all=True)
  2. {
  3. static $arrTree; //使用static代替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]); //注销当前节点数据,减少已无用的遍历
  15. self::getMenuTree($arrCat, $value[ 'cid'], $level);
  16. } // www.plcxue.com
  17. }
  18. return $arrTree;
  19. }
复制代码

使用以上方法,需要把分类写到php缓存文件中。

2、缓存文件写入方法:

  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. }
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn