本文实例讲述了php+mysql实现的无限分类方法类定义与使用。分享给大家供大家参考,具体如下:
创建数据库以及表
create databa `sortclass`default chart 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 chart=utf8;
header("content-type: text/html; chart=utf-8");//连接数据库$link = mysql_connect('localhost','root','eric') or die(mysql_error());mysql_lect_d蝉联往复b('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->tnode(0, -1, '顶极节点'); $this->link = $link; $this->table = $table; $node = array(); $results = mysql_query('lect * from '.$this->table.'',$this->link); while($node = mysql_fetch_assoc($results)){ $this->tnode($node['cid'],$node['pid'],$node['cname']); } } function tnode ($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] = !ist($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 = fal){ 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); ret($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("inrt into $this->table (`pid`,`cname`) values ('$pid','$name')",$this->link); } function modnode($cid, $newname){ mysql_query("update $this->table t `cname`='$newn快速阅读训练ame' where `cid` = $cid",$this->link); } function delnode($cid){ $allchilds = $this->getchilds($cid); $sql =''; if(empty($allchilds)){ $sql = "delete from $this->table where `cid` = $cid"; }el{ $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 t `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;}//声成lectfunction makelect($array,$formname){ global $tree; $lect = '<lect name="'.$formname.'">'; foreach ($array as $id){ $lect.='<option value="'.$id.'">'.$tree->getlayer($id, '|-').$tree->getvalue($id)."</option>"; } return $lect.'</lect>';}$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" /> 添加到:<?=makelect($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"> <?=makelect($category,'who')?>移动到:<?=makelect($category,'to')?> <input type="hidden" id="op" name="op" value="move" /> <input type="submit" value="submit" /> </form> <ul><?phpforeach ($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>
本文发布于:2023-04-08 17:58:00,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/6342d0a410fa87a460b35be6234dbb87.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:php+mysql实现的无限分类方法类定义与使用示例.doc
本文 PDF 下载地址:php+mysql实现的无限分类方法类定义与使用示例.pdf
留言与评论(共有 0 条评论) |