创建数据库以及表

 CREATE DATABASE `sortclass`DEFAULT CHARSET 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 CHARSET=utf8;
header("Content-type: text/html; charset=utf-8");
//连接数据库
$link = mysql_connect('localhost','root','eric') or die(mysql_error());
mysql_select_db('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->setNode(0, -1, '顶极节点');
$this->link = $link;
$this->table = $table;
$node = array();
$results = mysql_query('select * from '.$this->table.'',$this->link);
while($node = mysql_fetch_assoc($results)){
$this->setNode($node['cid'],$node['pid'],$node['cname']);
}
}
function setNode ($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] = !isset($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 = false){
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);
reset($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("insert into $this->table (`pid`,`cname`) values ('$pid','$name')",$this->link);
}
function modNode($cid, $newName){
mysql_query("update $this->table set `cname`='$newName' where `cid` = $cid",$this->link);
}
function delNode($cid){
$allChilds = $this->getChilds($cid);
$sql ='';
if(empty($allChilds)){
$sql = "delete from $this->table where `cid` = $cid";
}else{
$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 set `pid`=$topid where `cid` = $cid", $this->link);
}
}
//函数
function back(){
echo '<script language="JavaScript">window.location.href="test.php?"+new Date().getTime();</script>';
exit;
}
//声成select
function makeSelect($array,$formName){
global $tree;
$select = '<select name="'.$formName.'">';
foreach ($array as $id){
$select.='<option value="'.$id.'">'.$tree->getLayer($id, '|-').$tree->getValue($id)."</option>";
}
return $select.'</select>';
}
$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" /> 添加到:<?=makeSelect($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">
<?=makeSelect($category,'who')?>移动到:<?=makeSelect($category,'to')?>
<input type="hidden" id="op" name="op" value="move" />
<input type="submit" value="Submit" />
</form>
<ul>
<?php
foreach ($category as $id){
echo '<li>'.$tree->getLayer($id, '|- ').$tree->getValue($id).' <a href="test.php?op=del&cid='.$id.'">Del</a> <a onclick="mod('.$id.')">Edit</a> </li>';
}
?>
</ul>

php无限分类方法类的更多相关文章

  1. PHP+Mysql无限分类的方法汇总

    无限分类是个老话题了,来看看PHP结合Mysql如何实现.第一种方法这种方法是很常见.很传统的一种,先看表结构表:categoryid int 主键,自增name varchar 分类名称pid in ...

  2. php递归无限分类、根据子类获取所有顶类

    //递归无限分类树 public static function diGui($data, $pid) { $arr = collect([]); if (empty($data)) { return ...

  3. php不用递归完成无限分类,从表设计入手完整演示过程

    无限分类是什么就不废话了,可以用递归实现,但是递归从数据库取东西用递归效率偏低,如果从表设计入手,就很容易做到网站导航的实现,下面是某论坛导航,如下图 网上无限分类大多不全面,今天我会从设计表开始, ...

  4. laravel-nestedset:多级无限分类正确姿势

    laravel-nestedset:多级无限分类正确姿势   laravel-nestedset是一个关系型数据库遍历树的larvel4-5的插件包 目录: Nested Sets Model简介 安 ...

  5. MySql无限分类数据结构--预排序遍历树算法

    MySql无限分类数据结构--预排序遍历树算法 无限分类是我们开发中非常常见的应用,像论坛的的版块,CMS的类别,应用的地方特别多. 我们最常见最简单的方法就是在MySql里ID ,parentID, ...

  6. php递归方法实现无限分类实例

    数组:  代码如下 复制代码 $items = array( array('id' => 1, 'pid' => 0, 'name' => '一级11' ), array('id' ...

  7. [Objective-c 基础 - 2.8] category分类/类别/类目

    A.给某个类扩充方法(不改变原来的类) 例如,给类Person加上名为Simon的category,加上一个-study方法 使用()注明 Person+Simon.h @interface Pers ...

  8. tensorflow实现基于LSTM的文本分类方法

    tensorflow实现基于LSTM的文本分类方法 作者:u010223750 引言 学习一段时间的tensor flow之后,想找个项目试试手,然后想起了之前在看Theano教程中的一个文本分类的实 ...

  9. CSS规范 - 分类方法

    CSS文件的分类和引用顺序 通常,一个项目我们只引用一个CSS,但是对于较大的项目,我们需要把CSS文件进行分类. 我们按照CSS的性质和用途,将CSS文件分成“公共型样式”.“特殊型样式”.“皮肤型 ...

随机推荐

  1. Sequelize模糊查询

    const Sequelize = require('sequelize'); const Op = Sequelize.Op; User.findAll({ raw: true, order: [ ...

  2. Java13新特性 -- 文本块

    在JDK 12中引入了Raw String Literals特性,但在发布之前就放弃了.这个JEP与引入多行字符串文字(text block) 在意义上是类似的. 这条新特性跟 Kotlin 里的文本 ...

  3. ubuntu16.04下安装运行PL-SLAM

    PL-SLAM是Ruben Gomez-Ojeda大神融合点和线特征SLAM的最新成果,并开放了源代码,本博文记录安装运行PL-SLAM遇到的一些问题. 1源代码地址 https://github.c ...

  4. Callback must be a function. Received undefined

    fs.js:143 throw new ERR_INVALID_CALLBACK(cb); ^ TypeError [ERR_INVALID_CALLBACK]: Callback must be a ...

  5. Flink Table Api & SQL 翻译目录

    Flink 官网 Table Api & SQL  相关文档的翻译终于完成,这里整理一个安装官网目录顺序一样的目录 [翻译]Flink Table Api & SQL —— Overv ...

  6. 第1部分 Elasticsearch基础

    一.安装 es端口:9200 kibana端口:5601 brew install elasticsearch brew install elasticsearch brew services sta ...

  7. 使用HOSTNAMECTL配置主机名

    hostnamectl工具是用来管理给定主机中. 查看所有主机名 请运行下面的命令查看所有当前主机名: 〜] $ hostnamectl status 如果未指定任何选项,默认则使用status选项对 ...

  8. java字符串截取

    import org.apache.commons.lang.StringUtils; public class substr{ public static void main(String[] ar ...

  9. git revert commitid

    是生成一个和commitid的提交完全相反的提交.类似倒转.

  10. layui 自定义模块

    新建模块目录modules 新建common.js layui.define(['jquery'], function (exports) { var $ = layui.jquery; var ob ...