php+mysql数据库无限分类代码

本款php无限分类代码比较完整理包括了数据库是mysql的,有增加、删除、编辑、移动的功能,同时还提供数据库sql表结构.代码如下:

  1. //连接数据库
  2. $link = mysql_connect('localhost','root','') or die(mysql_error());
  3. mysql_select_db('class',$link)or die(mysql_error());
  4. mysql_query("set names gbk");
  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_array($results)){
  20. $this->setnode($node['id'],$node['f_id'],$node['name']);
  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. //echo "insert into $this->table (`f_id`,`name`) values ('$pid','$name')";exit;
  58. mysql_query("insert into $this->table (`f_id`,`name`) values ('$pid','$name')",$this->link);
  59. }
  60. function modnode($cid, $newname){
  61. mysql_query("update $this->table set `name`='$newname' where `id` = $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 `id` = $cid";
  68. }else{
  69. $sql = 'delete from '.$this->table.' where `id` in ('.implode(',',$allchilds).','.$cid.')';
  70. }
  71. mysql_query($sql,$this->link);
  72. }
  73. function movenode($cid, $topid){
  74. mysql_query("update $this->table set `f_id`=$topid where `id` = $cid", $this->link);
  75. }
  76. }
  77. //函数
  78. function back(){
  79. echo '<script language="网页特效">window.location.href="news.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,'`p_newsclass`');
  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();//开源代码phpfensi.com
  109. }
  110. }
  111. $category = $tree->getchilds();
  112. ?>

前台调用实例代码如下:

  1. <style type="text/css">
  2. body{font-size:12px;}
  3. ul{list-style:none;}
  4. a{cursor:pointer;}
  5. </style>
  6. <script language="javascript">
  7. function $(e){return document.getelementbyid(e);}
  8. function mod(cid){
  9. $('cid').value=cid;
  10. $('op').value='mod';
  11. $('name').style.;
  12. }
  13. </script>
  14. <form action="" method="post">
  15. 名称:<input type="text" name="cname" /> 添加到:<?=makeselect($category,'pid')?><br />
  16. <input type="hidden" name="op" value="add" />
  17. <input type="hidden" name="cid" />
  18. <input type="submit" value="submit" />
  19. </form>
  20. <h3>移动分类</h3>
  21. <form action="" method="post">
  22. <?=makeselect($category,'who')?> gt;移动到:<?=makeselect($category,'to')?>
  23. <input type="hidden" name="op" value="move" />
  24. <input type="submit" value="submit" />
  25. </form>
  26. <ul>
  27. <?php
  28. foreach ($category as $id){
  29. echo '<li>'.$tree->getlayer($id, '|- ').$tree->getvalue($id).' <a href="time.php?op=del&c">del</a> <a onclick="mod('.$id.')">edit</a> </li>';
  30. }
  31. ?>
  32. </ul>

用phpmyadmin导入此数据库就ok了,实例代码如下:

  1. -- phpmyadmin sql dump
  2. -- version 3.2.4
  3. -- http://www.phpfensi.com
  4. --
  5. -- 主机: localhost
  6. -- 生成日期: 2010 年 07 月 02 日 03:02
  7. -- 服务器版本: 5.1.41
  8. -- php 版本: 5.3.1
  9. set sql_mode="no_auto_value_on_zero";
  10. /*!40101 set @old_character_set_client=@@character_set_client */;
  11. /*!40101 set @old_character_set_results=@@character_set_results */;
  12. /*!40101 set @old_collation_connection=@@collation_connection */;
  13. /*!40101 set names utf8 */;
  14. --
  15. -- 数据库: `class`
  16. --
  17. -- --------------------------------------------------------
  18. --
  19. -- 表的结构 `p_newsclass`
  20. --
  21. create table if not exists `p_newsclass` (
  22. `id` int(7) not null auto_increment,
  23. `f_id` int(7) not null,
  24. `name` varchar(255) not null,
  25. primary key (`id`)
  26. ) engine=innodb default charset=utf8 auto_increment=10 ;
  27. --
  28. -- 转存表中的数据 `p_newsclass`
  29. --
  30. insert into `p_newsclass` (`id`, `f_id`, `name`) values
  31. (3, 0, '中国'),
  32. (4, 3, '福建'),
  33. (5, 4, '龙岩市'),
  34. (7, 4, '厦门市'),
  35. (9, 5, '漳平市');
  36. /*!40101 set character_set_client=@old_character_set_client */;
  37. /*!40101 set character_set_results=@old_character_set_results */;
  38. /*!40101 set collation_connection=@old_collation_connection */;