• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php+mysql数据库实现无限分类的方法_php技巧

php 搞代码 4年前 (2022-01-26) 28次浏览 已收录 0个评论

本文实例讲述了php+mysql数据库实现无限分类的方法。分享给大家供大家参考。具体分析如下:

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

//连接数据库 <br />$link = mysql_connect('localhost','root','') or die(mysql_error()); <br />mysql_select_db('class',$link)or die(mysql_error()); <br />mysql_query("set names gbk"); <br />//无限分类类库 <br />class sortclass{ <br />var $data = array(); <br />var $child = array(-1=>array()); <br />var $layer = array(-1=>-1); <br />var $parent = array(); <br />var $link; <br />var $table; <br />function sortclass($link, $table){ <br />$this->setnode(0, -1, '顶极节点'); <br />$this->link = $link; <br />$this->table = $table; <br />$node = array(); <br />$results = mysql_query("select * from $this->table",$this->link); <br />while($node = mysql_fetch_array($results)){ <br />$this->setnode($node['id'],$node['f_id'],$node['name']); <br />} <br />} <br />function setnode ($id, $parent, $value){ <br />$parent = $parent?$parent:0; <br />$this->data[$id] = $value; <br />$this->child[$id] = array(); <br />$this->child[$parent][] = $id; <br />$this->parent[$id] = $parent; <br />$this->layer[$id] = !isset($this->layer[$parent])? 0 : $this->layer[$parent] + 1; <br />} <br />function getlist (&$tree, $root= 0){ <br />foreach ($this->child[$root] as $key=>$id){ <br />$tree[] = $id; <br />if ($this->child[$id]) $this->getlist($tree, $id); <br />} <br />} <br />function getvalue ($id){return $this->data[$id];} <br />function getlayer ($id, $space = false){ <br />return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id]; <br />} <br />function getparent ($id){return $this->parent[$id];} <br />function getparents ($id){ <br />while ($this->parent[$id] != -1){ <br />$id = $parent[$this->layer[$id]] = $this->parent[$id]; <br />} <br />ksort($parent); <br />reset($parent); <br />return $parent; <br />} <br />function getchild ($id){return $this->child[$id];} <br />function getchilds ($id = 0){ <br />$child = array($id); <br />$this->getlist($child, $id); <br />return $child; <br />} <br />function addnode($name,$pid){ <br />//echo "insert into $this->table (`f_id`,`name`) values ('$pid','$name')";exit; <br />mysql_query("insert into $this->table (`f_id`,`name`) values ('$pid','$name')",$this->link); <br />} <br />function modnode($cid, $newname){ <br />mysql_query("update $this->table set `name`='$newname' where `id` = $cid",$this->link); <br />} <br />function delnode($cid){ <br />$allchilds = $this->getchilds($cid); <br />$sql =''; <br />if(emptyempty($allchilds)){ <br />$sql = "delete from $this->table where `id` = $cid"; <br />}else{ <br />$sql = 'delete from '.$this->table.' where `id` in ('.implode(',',$allchilds).','.$cid.')'; <br />} <br />mysql_query($sql,$this->link); <br />} <br />fu<em>/本2文来源[email protected]搞@^&代*@码2网</em><strong>搞gaodaima代码</strong>nction movenode($cid, $topid){ <br />mysql_query("update $this->table set `f_id`=$topid where `id` = $cid", $this->link); <br />} <br />} <br />//函数 <br />function back(){ <br />echo '<script language="网页特效">window.location.href="news.class.php?"+new date().gettime();</script>'; <br />exit; <br />} <br />//生成select <br />function makeselect($array,$formname){ <br />global $tree; <br />$select = ''; <br />foreach ($array as $id){ <br />$select.=''.$tree->getlayer($id, '|-').$tree->getvalue($id).""; <br />} <br />return $select.''; <br />} <br />$tree = new sortclass($link,'`p_newsclass`'); <br />$op = !emptyempty($_post['op']) ? $_post['op'] : $_get['op']; <br />if(!emptyempty($op)){ <br />if($op=='add'){ <br />$tree->addnode($_post['cname'],$_post['pid']); <br />back(); <br />} <br />if($op=='mod'){ <br />$tree->modnode($_post['cid'],$_post['cname']); <br />back(); <br />} <br />if($op=='del'){ <br />$tree->delnode($_get['cid']); <br />back(); <br />} <br />if($op=='move'){ <br />$tree->movenode($_post['who'],$_post['to']); <br />back();<br />} <br />} <br />$category = $tree->getchilds(); <br />?>

前台调用实例代码如下:

<style type="text/css"> <br />body{font-size:12px;} <br />ul{list-style:none;} <br />a{cursor:pointer;} <br /></style> <br /><script language="javascript"> <br />function $(e){return document.getelementbyid(e);} <br />function mod(cid){ <br />$('cid').value=cid; <br />$('op').value='mod'; <br />$('name').style.border='1px solid red'; <br />} <br /></script> <br /> <br />名称: 添加到:<br /> <br /> <br /> <br /> <br /> <br /><h3>移动分类</h3> <br /> <br /> gt;移动到: <br /> <br /> <br /> <br /><ul> <br /><?php <br />foreach ($category as $id){ <br />echo '<li>'.$tree->getlayer($id, '|- ').$tree->getvalue($id).' del edit </li>'; <br />} <br />?> <br /></ul>

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

-- phpmyadmin sql dump <br />-- version 3.2.4 <br />-- <br />-- 主机: localhost <br />-- 生成日期: 2010 年 07 月 02 日 03:02 <br />-- 服务器版本: 5.1.41 <br />-- php 版本: 5.3.1 <br />set sql_mode="no_auto_value_on_zero"; <br /> <br />/*!40101 set @old_character_set_client=@@character_set_client */; <br />/*!40101 set @old_character_set_results=@@character_set_results */; <br />/*!40101 set @old_collation_connection=@@collation_connection */; <br />/*!40101 set names utf8 */; <br />-- <br />-- 数据库: `class` <br />-- <br />-- -------------------------------------------------------- <br />-- <br />-- 表的结构 `p_newsclass` <br />-- <br />create table if not exists `p_newsclass` ( <br />  `id` int(7) not null auto_increment, <br />  `f_id` int(7) not null, <br />  `name` varchar(255) not null, <br />  primary key (`id`) <br />) engine=innodb  default charset=utf8 auto_increment=10 ; <br />-- <br />-- 转存表中的数据 `p_newsclass` <br />-- <br />insert into `p_newsclass` (`id`, `f_id`, `name`) values <br />(3, 0, '中国'), <br />(4, 3, '福建'), <br />(5, 4, '龙岩市'), <br />(7, 4, '厦门市'), <br />(9, 5, '漳平市'); <br />/*!40101 set character_set_client=@old_character_set_client */; <br />/*!40101 set character_set_results=@old_character_set_results */; <br />/*!40101 set collation_connection=@old_collation_connection */;

希望本文所述对大家的PHP+mysql程序设计有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php+mysql数据库实现无限分类的方法_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址