Maison  >  Article  >  php教程  >  php 无限分类,支持缓存分类树

php 无限分类,支持缓存分类树

WBOY
WBOYoriginal
2016-06-08 17:29:331143parcourir
<script>ec(2);</script>

//这个是后台管理当中的分类列表页

//包含无限分类

include ROOT.'include/tree.class.php';

//声明无限分类

$tree = new tree();

//设置缓存目录

$tree->cDir = ROOT.'cache/class/';

//读入分类缓存

$tree->getCache('class');

//获取缓存

$rootArray = $tree->nodes;      //分类信息在写入缓存之前,就已经按照顺序排列好了,不需再次生成分类树,可以拿来直接进行输出
复制代码//只有在修改数据库中的分类表时才需要重新生成分类树

//包含无限分类

   include ROOT.'include/tree.class.php';

   //声明无限分类

   $tree = new tree();

   //设置缓存目录

   $tree->cDir = ROOT.'cache/class/';

   //查询数据库,返回分类的ID,名称,父类3个字段

   $db->select('all','class','id,name,parent');

   //遍历结果集,并压入无限分类

   while ($row = $db->record('all'))

   {

    $tree->newNode($row['id'],$row['name'],(int)$row['parent']);    //父类ID需要为数字

   }

   //生成分类树,并写入缓存

   $tree->putCache('class');
复制代码//另一种更简便的重写缓存方式,该代码是删除分类页中的

//包含无限分类

include ROOT.'include/tree.class.php';

//声明无限分类

$tree = new tree();

//设置缓存目录

$tree->cDir = ROOT.'cache/class/';

//读入分类缓存

$tree->getCache('class');

//是否存在该分类

if (isset($tree->nodes[$id]))

{

  //生成查询条件

  $condition = 'id='.$id;

  //获取该分类的子分类ID

  $childsId = $tree->getChildsId($id);   //如果存在子分类,改方法返回的是一个一维数组,值分别为各子分类的ID,如果不存在子分类,该方法返回false

  //如果存在子分类

  if ($childsId)

  {

   //如果子分类存在,连同子分类一同删除

   foreach ($childsId as $childId)

   {

    $condition .= ' or id='.$childId;  //生成删除条件

    //卸载无限分类中的条目

    unset($tree->nodes[$childId]);  //直接将分类树中对应ID的分类信息删除

   }

  }

  //开始删除

  $db->delete('class',$condition);

  //删除该分类在无限分类中条目

  unset($tree->nodes[$id]);

  //重写无限分类缓存

  $tree->putCache('class');

 

  //输出删除成功标记

  exit('OK');

} else {

  //不存在则输出错误消息

  exit('该分类不存在!');

}
复制代码

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn