网站地图    收藏   

主页 > php专栏 > php综合实列 >

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

来源:自学PHP网    时间:2014-12-02 13:09 作者: 阅读:次

[导读] ?php 连接数据库$link=mysql_connect( 39;localhost 39;, 39;root 39;, 39;密码 39;)ordie(mysql_error());mysql_select_db( 39;s...

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

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

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论