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

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

创建数据库以及表

  1. CREATE DATABASE `sortclass`DEFAULT CHARSET utf8;
  2. CREATE TABLE IF NOT EXISTS `class` (
  3. `cid` mediumint(8) unsigned NOT NULL auto_increment,
  4. `pid` mediumint(8) unsigned NOT NULL,
  5. `cname` varchar(50) NOT NULL,
  6. PRIMARY KEY (`cid`),
  7. KEY `pid` (`pid`)
  8. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

PHP代码:

  1. header("Content-type: text/html; charset=utf-8");
  2. //连接数据库
  3. $link = mysql_connect('localhost','root','eric') or die(mysql_error());
  4. mysql_select_db('sortclass',$link);
  5. //无限分类类库
  6. class SortClass{
  7. var $data = array();
  8. var $child = array(-1=>array());
  9. var $layer = array(-1=>-1);
  10. var $parent = array();
  11. var $link;
  12. var $table;
  13. function SortClass($link, $table){
  14. $this->setNode(0, -1, '顶极节点');
  15. $this->link = $link;
  16. $this->table = $table;
  17. $node = array();
  18. $results = mysql_query('select * from '.$this->table.'',$this->link);
  19. while($node = mysql_fetch_assoc($results)){
  20. $this->setNode($node['cid'],$node['pid'],$node['cname']);
  21. }
  22. }
  23. function setNode ($id, $parent, $value){
  24. $parent = $parent?$parent:0;
  25. $this->data[$id] = $value;
  26. $this->child[$id] = array();
  27. $this->child[$parent][] = $id;
  28. $this->parent[$id] = $parent;
  29. $this->layer[$id] = !isset($this->layer[$parent])? 0 : $this->layer[$parent] + 1;
  30. }
  31. function getList (&$tree, $root= 0){
  32. foreach ($this->child[$root] as $key=>$id){
  33. $tree[] = $id;
  34. if ($this->child[$id]) $this->getList($tree, $id);
  35. }
  36. }
  37. function getValue ($id){return $this->data[$id];}
  38. function getLayer ($id, $space = false){
  39. return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id];
  40. }
  41. function getParent ($id){return $this->parent[$id];}
  42. function getParents ($id){
  43. while ($this->parent[$id] != -1){
  44. $id = $parent[$this->layer[$id]] = $this->parent[$id];
  45. }
  46. ksort($parent);
  47. reset($parent);
  48. return $parent;
  49. }
  50. function getChild ($id){return $this->child[$id];}
  51. function getChilds ($id = 0){
  52. $child = array($id);
  53. $this->getList($child, $id);
  54. return $child;
  55. }
  56. function addNode($name,$pid){
  57. mysql_query("insert into $this->table (`pid`,`cname`) values ('$pid','$name')",$this->link);
  58. }
  59. function modNode($cid, $newName){
  60. mysql_query("update $this->table set `cname`='$newName' where `cid` = $cid",$this->link);
  61. }
  62. function delNode($cid){
  63. $allChilds = $this->getChilds($cid);
  64. $sql ='';
  65. if(emptyempty($allChilds)){
  66. $sql = "delete from $this->table where `cid` = $cid";
  67. }else{
  68. $sql = 'delete from '.$this->table.' where `cid` in ('.implode(',',$allChilds).','.$cid.')';
  69. }
  70. mysql_query($sql,$this->link);
  71. }
  72. function moveNode($cid, $topid){
  73. mysql_query("update $this->table set `pid`=$topid where `cid` = $cid", $this->link);
  74. }
  75. }
  76. //函数
  77. function back(){
  78. echo '<script language="JavaScript">window.location.href="test.php?" rel="external nofollow" +new Date().getTime();</script>';
  79. exit;
  80. }
  81. //声成select
  82. function makeSelect($array,$formName){
  83. global $tree;
  84. $select = '<select name="'.$formName.'">';
  85. foreach ($array as $id){
  86. $select.='<option value="'.$id.'">'.$tree->getLayer($id, '|-').$tree->getValue($id)."</option>";
  87. }
  88. return $select.'</select>';
  89. }
  90. $tree = new SortClass($link,'`class`');
  91. $op = !emptyempty($_POST['op']) ? $_POST['op'] : $_GET['op'];
  92. if(!emptyempty($op)){
  93. if($op=='add'){
  94. $tree->addNode($_POST['cname'],$_POST['pid']);
  95. back();
  96. }
  97. if($op=='mod'){
  98. $tree->modNode($_POST['cid'],$_POST['cname']);
  99. back();
  100. }
  101. if($op=='del'){
  102. $tree->delNode($_GET['cid']);
  103. back();
  104. }
  105. if($op=='move'){
  106. $tree->moveNode($_POST['who'],$_POST['to']);
  107. back();
  108. }
  109. }
  110. $category = $tree->getChilds();
  111. ?>
  112. <style type="text/css">
  113. body{font-size:12px;}
  114. ul{list-style:none;}
  115. a{cursor:pointer;}
  116. </style>
  117. <script language="javascript">
  118. function $(e){return document.getElementById(e);}
  119. function mod(cid){
  120. $('cid').value=cid;
  121. $('op').value='mod';
  122. $('name').style.;
  123. }
  124. </script>
  125. <form action="test.php" method="post">
  126. 名称:<input type="text" name="cname" /> 添加到:<?=makeSelect($category,'pid')?><br />
  127. <input type="hidden" name="op" value="add" />
  128. <input type="hidden" name="cid" />
  129. <input type="submit" value="Submit" />
  130. </form>
  131. <h3>移动分类</h3>
  132. <form action="test.php" method="post">
  133. <?=makeSelect($category,'who')?>移动到:<?=makeSelect($category,'to')?>
  134. <input type="hidden" name="op" value="move" />
  135. <input type="submit" value="Submit" />
  136. </form>
  137. <ul>
  138. <?php
  139. foreach ($category as $id){
  140. echo '<li>'.$tree->getLayer($id, '|- ').$tree->getValue($id).' <a href="test.php?op=del&c" rel="external nofollow" >Del</a> <a onclick="mod('.$id.')">Edit</a> </li>';
  141. }
  142. ?>
  143. </ul>