php+mysql实现的无限分类方法类定义与使用示例


Posted in PHP onMay 27, 2020

本文实例讲述了php+mysql实现的无限分类方法类定义与使用。分享给大家供大家参考,具体如下:

创建数据库以及表

CREATE DATABASE `sortclass`DEFAULT CHARSET utf8;
 CREATE TABLE IF NOT EXISTS `class` (
 `cid` mediumint(8) unsigned NOT NULL auto_increment,
 `pid` mediumint(8) unsigned NOT NULL,
 `cname` varchar(50) NOT NULL,
 PRIMARY KEY (`cid`),
 KEY `pid` (`pid`)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
header("Content-type: text/html; charset=utf-8");
//连接数据库
$link = mysql_connect('localhost','root','eric') or die(mysql_error());
mysql_select_db('sortclass',$link);
//无限分类类库
class SortClass{
 var $data = array();
 var $child = array(-1=>array());
 var $layer = array(-1=>-1);
 var $parent = array();
 var $link;
 var $table;
 function SortClass($link, $table){
  $this->setNode(0, -1, '顶极节点');
  $this->link = $link;
  $this->table = $table;
  $node = array();
  $results = mysql_query('select * from '.$this->table.'',$this->link);
  while($node = mysql_fetch_assoc($results)){
   $this->setNode($node['cid'],$node['pid'],$node['cname']);
  }
 }
 function setNode ($id, $parent, $value){
  $parent = $parent?$parent:0;
  $this->data[$id] = $value;
  $this->child[$id] = array();
  $this->child[$parent][] = $id;
  $this->parent[$id] = $parent;
  $this->layer[$id] = !isset($this->layer[$parent])? 0 : $this->layer[$parent] + 1;
 }
 function getList (&$tree, $root= 0){
  foreach ($this->child[$root] as $key=>$id){
   $tree[] = $id;
   if ($this->child[$id]) $this->getList($tree, $id);
  }
 }
 function getValue ($id){return $this->data[$id];}
 function getLayer ($id, $space = false){
  return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id];
 }
 function getParent ($id){return $this->parent[$id];}
 function getParents ($id){
  while ($this->parent[$id] != -1){
   $id = $parent[$this->layer[$id]] = $this->parent[$id];
  }
  ksort($parent);
  reset($parent);
  return $parent;
 }
 function getChild ($id){return $this->child[$id];}
 function getChilds ($id = 0){
  $child = array($id);
  $this->getList($child, $id);
  return $child;
 }
 function addNode($name,$pid){
  mysql_query("insert into $this->table (`pid`,`cname`) values ('$pid','$name')",$this->link);
 }
 function modNode($cid, $newName){
  mysql_query("update $this->table set `cname`='$newName' where `cid` = $cid",$this->link);
 }
 function delNode($cid){
  $allChilds = $this->getChilds($cid);
  $sql ='';
  if(empty($allChilds)){
   $sql = "delete from $this->table where `cid` = $cid";
  }else{
   $sql = 'delete from '.$this->table.' where `cid` in ('.implode(',',$allChilds).','.$cid.')';
  }
  mysql_query($sql,$this->link);
 }
 function moveNode($cid, $topid){
  mysql_query("update $this->table set `pid`=$topid where `cid` = $cid", $this->link);
 }
}
//函数
function back(){
 echo '<script language="JavaScript">window.location.href="test.php?" rel="external nofollow" +new Date().getTime();</script>';
 exit;
}
//声成select
function makeSelect($array,$formName){
 global $tree;
 $select = '<select name="'.$formName.'">';
 foreach ($array as $id){
  $select.='<option value="'.$id.'">'.$tree->getLayer($id, '|-').$tree->getValue($id)."</option>";
 }
 return $select.'</select>';
}
$tree = new SortClass($link,'`class`');
$op = !empty($_POST['op']) ? $_POST['op'] : $_GET['op'];
if(!empty($op)){

 if($op=='add'){
  $tree->addNode($_POST['cname'],$_POST['pid']);
  back();
 }

 if($op=='mod'){
  $tree->modNode($_POST['cid'],$_POST['cname']);
  back();
 }

 if($op=='del'){
  $tree->delNode($_GET['cid']);
  back();
 }

 if($op=='move'){
  $tree->moveNode($_POST['who'],$_POST['to']);
  back();
 }
}
$category = $tree->getChilds();
?>
 <style type="text/css">
  body{font-size:12px;}
  ul{list-style:none;}
  a{cursor:pointer;}
 </style>
 <script language="javascript">
  function $(e){return document.getElementById(e);}
  function mod(cid){
   $('cid').value=cid;
   $('op').value='mod';
   $('name').style.border='1px solid red';
  }
 </script>
 <form action="test.php" method="post">
  名称:<input type="text" id="name" name="cname" /> 添加到:<?=makeSelect($category,'pid')?><br />
  <input type="hidden" id="op" name="op" value="add" />
  <input type="hidden" id="cid" name="cid" />
  <input type="submit" value="Submit" />
 </form>
 <h3>移动分类</h3>
 <form action="test.php" method="post">
  <?=makeSelect($category,'who')?>移动到:<?=makeSelect($category,'to')?>
  <input type="hidden" id="op" name="op" value="move" />
  <input type="submit" value="Submit" />
 </form>
 <ul>
<?php
foreach ($category as $id){
 echo '<li>'.$tree->getLayer($id, '|- ').$tree->getValue($id).' <a href="test.php?op=del&cid='.$id.'" rel="external nofollow" >Del</a> <a onclick="mod('.$id.')">Edit</a> </li>';
}
?>
</ul>

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
php5.3中连接sqlserver2000的两种方法(com与ODBC)
Dec 29 PHP
解析MySql与Java的时间类型
Jun 22 PHP
九个你必须知道而且又很好用的php函数和特点
Aug 08 PHP
Server.HTMLEncode让代码在页面里显示为源代码
Dec 08 PHP
Destoon实现多表查询示例
Aug 21 PHP
PHP使用CURL模拟登录的方法
Jul 08 PHP
php 获取文件行数的方法总结
Oct 11 PHP
php正则提取html图片(img)src地址与任意属性的方法
Feb 08 PHP
基于ThinkPHP实现的日历功能实例详解
Apr 15 PHP
laravel 5异常错误:FatalErrorException in Handler.php line 38的解决
Oct 12 PHP
Yii框架使用PHPExcel导出Excel文件的方法分析【改进版】
Jul 24 PHP
PHP命名空间用法实例分析
Sep 04 PHP
php与阿里云短信接口接入操作案例分析
May 27 #PHP
PHP开发API接口签名生成及验证操作示例
May 27 #PHP
php+websocket 实现的聊天室功能详解
May 27 #PHP
php+js实现的拖动滑块验证码验证表单操作示例【附源码下载】
May 27 #PHP
PHP code 验证码生成类定义和简单使用示例
May 27 #PHP
PHP 计算至少是其他数字两倍的最大数的实现代码
May 26 #PHP
tp5.1 框架数据库-数据集操作实例分析
May 26 #PHP
You might like
php防止sql注入之过滤分页参数实例
2014/11/03 PHP
PHP实现超简单的SSL加密解密、验证及签名的方法示例
2017/08/28 PHP
基于jQuery的图片大小自动适应实现代码
2010/11/17 Javascript
javascript重写alert方法的实例代码
2013/03/29 Javascript
JQuery页面图片切换和新闻列表滚动效果的具体实现
2013/09/26 Javascript
js中同步与异步处理的方法和区别总结
2013/12/25 Javascript
巧用replace将文字表情替换为图片
2014/04/17 Javascript
基于Jquery实现表单验证
2020/07/20 Javascript
js生成随机数(指定范围)的实例代码
2016/07/10 Javascript
JavaScript 闭包机制详解及实例代码
2016/10/10 Javascript
JavaScript 事件对内存和性能的影响
2017/01/22 Javascript
jQuery获取Table某列的值(推荐)
2017/03/03 Javascript
vue loadmore 组件滑动加载更多源码解析
2017/07/19 Javascript
js微信应用场景之微信音乐相册案例分享
2017/08/11 Javascript
JS+CSS实现网页加载中的动画效果
2017/10/27 Javascript
jQuery实现倒计时功能完整示例
2020/06/01 jQuery
Python中集合类型(set)学习小结
2015/01/28 Python
自己使用总结Python程序代码片段
2015/06/02 Python
python使用sqlite3时游标使用方法
2018/03/13 Python
分享vim python缩进等一些配置
2018/07/02 Python
Python使用一行代码获取上个月是几月
2018/08/30 Python
Python魔法方法详解
2019/02/13 Python
对Python获取屏幕截图的4种方法详解
2019/08/27 Python
pygame实现烟雨蒙蒙下彩虹雨
2019/11/11 Python
如何基于python把文字图片写入word文档
2020/07/31 Python
开发人员所需要知道的HTML5性能分析面面观
2012/07/05 HTML / CSS
购买200个世界上最好的内衣品牌:Bare Necessities
2017/02/11 全球购物
欧洲最古老的鞋厂:Peter Kaiser
2019/11/05 全球购物
Nasty Gal英国:美国女性服饰销售网站
2021/03/02 全球购物
2019年.net常见面试问题
2012/02/12 面试题
公司部门司机岗位职责
2014/01/03 职场文书
企业文化演讲稿
2014/05/20 职场文书
高中综合实践活动总结
2014/07/07 职场文书
大学生简短的自我评价
2014/09/12 职场文书
2016年度基层党建工作公开承诺书
2016/03/25 职场文书
Python人工智能之混合高斯模型运动目标检测详解分析
2021/11/07 Python