A-A+

完整的新闻无限级分类代码,可添加,删除,移动,修改

2019年03月16日 我爱编程 暂无评论
  1. <?php
  2. //连接数据库
  3. $link = mysql_connect('localhost','root','密码'or die(mysql_error());
  4. mysql_select_db('sortclass',$link);
  5. mysql_query("set names 'gbk'");
  6. //无限分类类库 
  7. class sortclass{
  8. var $data = array();
  9. var $child = array(-1=>array());
  10. var $layer = array(-1=>-1);
  11. var $parent = array();
  12. var $link;
  13. var $table;
  14. function sortclass($link$table){
  15. $this->setnode(0, -1, '顶极节点');
  16. $this->link = $link;
  17. $this->table = $table;
  18. $node = array();
  19. $results = mysql_query('select * from '.$this->table.'',$this->link);
  20. while($node = mysql_fetch_assoc($results)){
  21. $this->setnode($node['cid'],$node['pid'],$node['cname']);
  22. }
  23. }
  24. function setnode ($id$parent$value){
  25. $parent = $parent?$parent:0;
  26. $this->data[$id] = $value;
  27. $this->child[$id] = array();
  28. $this->child[$parent][] = $id;
  29. $this->parent[$id] = $parent;
  30. $this->layer[$id] = !isset($this->layer[$parent])? 0 : $this->layer[$parent] + 1;
  31. }
  32. function getlist (&$tree$root= 0){
  33. foreach ($this->child[$rootas $key=>$id){
  34. $tree[] = $id;
  35. if ($this->child[$id]) $this->getlist($tree$id);
  36. }
  37. }
  38. function getvalue ($id){return $this->data[$id];}
  39. function getlayer ($id$space = false){
  40. return $space?str_repeat($space$this->layer[$id]):$this->layer[$id];
  41. }
  42. function getparent ($id){return $this->parent[$id];}
  43. function getparents ($id){
  44. while ($this->parent[$id] != -1){
  45. $id = $parent[$this->layer[$id]] = $this->parent[$id];
  46. }
  47. ksort($parent);
  48. reset($parent);
  49. return $parent;
  50. }
  51. function getchild ($id){return $this->child[$id];}
  52. function getchilds ($id = 0){
  53. $child = array($id);
  54. $this->getlist($child$id);
  55. return $child;
  56. }
  57. function addnode($name,$pid){
  58. mysql_query("insert into $this->table (`pid`,`cname`) values ('$pid','$name')",$this->link);
  59. }
  60. function modnode($cid$newname){
  61. mysql_query("update $this->table set `cname`='$newname' where `cid` = $cid",$this->link);
  62. }
  63. function delnode($cid){
  64. $allchilds = $this->getchilds($cid);
  65. $sql ='';
  66. if(emptyempty($allchilds)){
  67. $sql = "delete from $this->table where `cid` = $cid";
  68. }else{
  69. $sql = 'delete from '.$this->table.' where `cid` in ('.implode(',',$allchilds).','.$cid.')';
  70. }
  71. mysql_query($sql,$this->link);
  72. }
  73. function movenode($cid$topid){
  74. mysql_query("update $this->table set `pid`=$topid where `cid` = $cid"$this->link);
  75. }
  76. }
  77. //函数 
  78. function back(){
  79. echo '<script language="javascript">window.location.href="class.php?"+new date().gettime();</script>';
  80. exit;
  81. }
  82. //生成select 
  83. function makeselect($array,$formname){
  84. global $tree;
  85. $select = '<select name="'.$formname.'">';
  86. foreach ($array as $id){
  87. $select.='<option value="'.$id.'">'.$tree->getlayer($id'|-').$tree->getvalue($id)."</option>";
  88. }
  89. return $select.'</select>';
  90. }
  91. $tree = new sortclass($link,'`class`');
  92. $op = !emptyempty($_post['op']) ? $_post['op'] : $_get['op'];
  93. if(!emptyempty($op)){
  94. if($op=='add'){
  95. $tree->addnode($_post['cname'],$_post['pid']);
  96. back();
  97. }
  98. if($op=='mod'){
  99. $tree->modnode($_post['cid'],$_post['cname']);
  100. back();
  101. }
  102. if($op=='del'){
  103. $tree->delnode($_get['cid']);
  104. back();
  105. }
  106. if($op=='move'){
  107. $tree->movenode($_post['who'],$_post['to']);
  108. back();
  109. }
  110. }
  111. $category = $tree->getchilds();
  112. ?>
  113. <style type="text/css">
  114. body{font-size:12px;}
  115. ul{list-style:none;}
  116. a{cursor:pointer;}
  117. input{ margin-top:8px;}
  118. </style>
  119. <script language="javascript">
  120. function $(e){return document.getelementbyid(e);}
  121. function mod(cid){
  122. $('cid').value=cid;
  123. $('op').value='mod';
  124. $('name').style.border='1px solid red';
  125. }
  126. </script>
  127. <h3>添加分类</h3>
  128. <form action="class.php" method="post">
  129. 名称:<input type="text" id="name" name="cname" /> 添加到:<?=makeselect($category,'pid')?><br />
  130. <input type="hidden" id="op" name="op" value="add" />
  131. <input type="hidden" id="cid" name="cid" />
  132. <input type="submit" value="添加分类" />
  133. </form>
  134. <h3>移动分类</h3>
  135. <form action="class.php" method="post">
  136. <?=makeselect($category,'who')?>移动到:<?=makeselect($category,'to')?>
  137. <input type="hidden" id="op" name="op" value="move" />
  138. <input type="submit" value="移动" />
  139. </form>
  140. <ul>
  141. <?php
  142. foreach ($category as $id){
  143. echo '<li>'.$tree->getlayer($id'|- ').$tree->getvalue($id).' <a href="class.php?op=del&cid='.$id.'">del</a> <a onclick="mod('.$id.')">edit</a> </li>';
  144. }
  145. ?>
  146. </ul>

顶极节点 del edit  

|- 1级分类del   edit
|- 1级分类 del   edit
|- 1级分类 del   edit
|- |- 2级分类 del   edit
|- |- |- 3级分类 del  edit
|- |- |-|- 4级分类 del  edit
|- 1级分类 del   edit

数据库代码如下:

  1. <!-- 
  2. create database `sortclass`default charset utf8;
  3. create table if not exists `class` (
  4. `cid` mediumint(8) unsigned not null auto_increment,
  5. `pid` mediumint(8) unsigned not null,
  6. `cname` varchar(50) not null,
  7. primary key (`cid`),
  8. key `pid` (`pid`)
  9. ) engine=myisam default charset=utf8;
  10. ---> 

给我留言

Copyright © 四季博客 保留所有权利.   Theme  Ality

用户登录